Hello Can anyone tell me the way to reverse a string in C and only in """"C"""?

How to get a string through one by one characters of the same?
And if I use the following I get errors

String name="hai";

len=strlen(name);

for(i=0;i<len;i++)
{
scanf("%c",name);
}

how to get name one by one in C
but in java it is easy because using charAt() function i can get each cahracter of the string.But in C???

Maybe what you want is this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	int i,j;
	char *str="Good, Morning!";
	int len=strlen(str);

	char *Rstr=(char *)malloc(len+1);
	for(j=0,i=len-1;j<len;j++,i--)
		Rstr[j]=str[i];
	Rstr[j]='\0';

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

	return 0;
}
Comments
you forgot to comment each line, type up the documentation, burn a CD Rom, and overnight it to him via UPS
Don't answer questions with working code! We want people to learn, not cheat.

You can use 'strrev(char *str)' function directly or you will need 2 'for' loops - one for printing the input and the other for printing the reverse such that

for(i=0;i<n;i++)//To print the input
for(i=n;i>0;i--)//To print the reversed string

If u want to use 'strrev(char *str)' ,then u will need 'string.h' header file.

You can use 'strrev(char *str)' function directly or you will need 2 'for' loops - one for printing the input and the other for printing the reverse such that

for(i=0;i<n;i++)//To print the input
for(i=n;i>0;i--)//To print the reversed string

If u want to use 'strrev(char *str)' ,then u will need 'string.h' header file.

If you have a string in an array you can just print it out.

#include <stdio.h>

main(void)
{
  char *str = "Hello World!";

  printf("%s\n", str);
}

1) [for(i=0;i<n;i++)//To print the input
for(i=n;i>0;i--)//To print the reversed string]

To use this statement you have to take input as charecter but not as a string.

2) If you want to use "strrev(char *str)" function you can take input as a string.
Then it will reverse the string.

Comments
No need to post twice. And how does he learn the technique to reverse a string by using strrev?

String reverse without using additional space

1. Use strlen (or write your own function) to calculate the length of the string
2. x= strlen /2
3. from 0 to x, interchange the positions of the first and last, second and second last .... and so on

String name="hai";
try this code


len=strlen(name);

for(i=len;i>=0;i--)
{
print("%c",name[i]);
}

Edited 6 Years Ago by stokes1900: n/a

Comments
How does that actually reverse a string? It only prints.
char* my_strrev(char *p){
   int i,l,temp;
   for(l=0;*(p+l+1);l++);
   for(i=0;i<l;i++,l--){
     temp=p[i];
     p[i]=p[l];
     p[l]=temp;
   }
 return p;
}

This Function will work like strrev() Function...

int main()
{
    char *rec="minhaj akhter khan" ;
    int len,inc;

    len = strlen(rec);   


    while(len--)
    {
           printf("%c",*(rec+len));
    }

    return 0;

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.