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???

Recommended Answers

All 8 Replies

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;
}
commented: Don't answer questions with working code! We want people to learn, not cheat. -3
commented: you forgot to comment each line, type up the documentation, burn a CD Rom, and overnight it to him via UPS -2

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.

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

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]);
}
commented: How does that actually reverse a string? It only prints. -3
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;

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.