answersLogoWhite

0

Reverse a string in C language?

Updated: 8/11/2023
User Avatar

Wiki User

14y ago

Best Answer

/* To reverse the given string using pointer By $ */

#include <stdio.h>

#include <string.h>

void main ()

{

char str[15];

char rev[15];

int i;

int count;

char* p;

gets(str);

count = strlen(str);

p = rev + strlen(str)-1;

for(i = 0; i < count; i++)

{

*(p - i) = *(str + i);

}

rev[count]='\0';//in some compilers it may show error show u can make it NULL

/* verify results */

printf("Original string: %s\n",str);

printf("Reversed string: %s\n",rev);

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<string.h>

#include<conio.h>

void main()

{

int num,i,j,result,index;

char name[25][25],temp[25];

clrscr();

printf("\nNumber of names to be sorted in descending order\n");

scanf("%d",&num);

printf("Enter %d names to be sorted\n",num);

for(i=0;i<num;i++)

scanf("%s",name[i]);

for(i=0;i<num;i++)

{ index=i;

for(j=i+1;j<num;j++)

{

result=strcmp(name[index],name[j]);

if(result<0)

index=j;

}

strcpy(temp,name[index]);

strcpy(name[index],name[i]);

strcpy(name[i],temp);

}

printf("\nNames Sorted in Descending Order\n");

for(i=0;i<num;i++)

{

printf("\t%s",name[i]);

}

}

email ThisBlogThis!Share to TwitterShare to Facebook

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

reverse (char *p) { char c, *p1, *p2;

if (strlen(p) <= 1) return; for (p1=p, p2=p+strlen(p)-1; p1<p2; c=*p1, *p1=*p2, *p2=c, p1++, p2--); }

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

reverce input show

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Reverse a string in C language?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the use of Reverse String in C program?

The use of the reverse string in C program is used to reverse the letters in the string. An example would be reverse me would be reversed to em esrever.


How do you convert a number that consist of alphabet and number in reverse order?

To reverse a number, first convert the number to a string, then reverse the string. Given your number consists of alphanumeric characters, the number must already be a string so simply reverse the string: #include&lt;string&gt; using std::string; string reverse (const string&amp; s) { string str {}; for (auto c : s) str.insert (str.begin(), c); return str; } int main () { std::cout &lt;&lt; "Enter a number: "; string s {}; std::cin &gt;&gt; s; std::cout &lt;&lt; "The number in reverse is: " &lt;&lt; reverse (s); }


What is literal in c language?

a string constant


Write a flowchart of reverse the string in c?

wefwfe


How do you get value of a string in reverse order in c?

Assume C#, not C: Traditional way: public string Reverse(string s) { if (string.IsNullOrEmpty(s)) return s; // "" or null char[] characters = s.ToCharArray(); Array.Reverse(characters); return new string(characters); } or as an extension method: public static string Reverse(this string s) { if (s == "") return ""; char[] characters = s.ToCharArray(); Array.Reverse(characters); return new string(characters); } The differences of the 2 methods above is on the caller (how to use Reverse()), and they may co-exist: For example: string test = "abc"; string result1 = Reverse(test); // traditional way string result2 = test.Reverse(); // call the extension

Related questions

What is the use of Reverse String in C program?

The use of the reverse string in C program is used to reverse the letters in the string. An example would be reverse me would be reversed to em esrever.


How do you convert a number that consist of alphabet and number in reverse order?

To reverse a number, first convert the number to a string, then reverse the string. Given your number consists of alphanumeric characters, the number must already be a string so simply reverse the string: #include&lt;string&gt; using std::string; string reverse (const string&amp; s) { string str {}; for (auto c : s) str.insert (str.begin(), c); return str; } int main () { std::cout &lt;&lt; "Enter a number: "; string s {}; std::cin &gt;&gt; s; std::cout &lt;&lt; "The number in reverse is: " &lt;&lt; reverse (s); }


What is literal in c language?

a string constant


Write a flowchart of reverse the string in c?

wefwfe


How do you get value of a string in reverse order in c?

Assume C#, not C: Traditional way: public string Reverse(string s) { if (string.IsNullOrEmpty(s)) return s; // "" or null char[] characters = s.ToCharArray(); Array.Reverse(characters); return new string(characters); } or as an extension method: public static string Reverse(this string s) { if (s == "") return ""; char[] characters = s.ToCharArray(); Array.Reverse(characters); return new string(characters); } The differences of the 2 methods above is on the caller (how to use Reverse()), and they may co-exist: For example: string test = "abc"; string result1 = Reverse(test); // traditional way string result2 = test.Reverse(); // call the extension


Special character in C language?

special character in c language are as follows~ ' ! @ # % ^ &amp; * () _ - + = | \ {} [] : ; " &lt;&gt; , . ? /


Definition of buffer in assembly language?

its just like a string of c++


How to Program for reversal of string ven123kat in c?

#include #include #include int reverse(int i);char st[]="ven123kat";void main() {printf("\nThe string is: %s", st);reverse(0);printf("\nReversed string is: %s", st);getch();}int reverse(int i) {if (i


How do you make a program to reverse nummber in c language?

I'd use sprintf (assuming the number wasn't a string already) and pointers. If that's not enough of a clue, you're really not ready to be in this programming class.


How you can convert from string to int in C language?

Use the atoi() or atol() function.


How print the string with double cot in c plus plus language?

console.wrikerle("""");


Int a is literal in C language or not?

int a; -- variable definition"int a" -- string literal