top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What should be the best way to reverse a string in C without the use of strrev?

+3 votes
429 views
What should be the best way to reverse a string in C without the use of strrev?
posted Dec 3, 2015 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
Do u want to store the reversed string or just want to print in reverse order ?

3 Answers

0 votes

Take two pointers one at the start and one at the end, replace swap start and end character.
Move start pointer forward and end backward till start is less hen end.

void rverese_array(char arr[])
{
    int start = 0, end = strlen(arr) - 1;
    while (start < end)
    {
        char temp = arr[start];
        arr[start] = arr[end];
        arr[end] = temp;
        start++;
        end--;
    }
}
answer Dec 4, 2015 by Salil Agrawal
0 votes
#include<stdio.h>
#include<string.h>
main()
{
char a[]="Some String",b[length_a];
int i,j;
j=strlen(a)-1;
for(i=0;i<j;i++,j--)
    b[i]=a[j];
b[i]='\0';
printf("Reverse string = %s \n",b);
}
answer Dec 4, 2015 by Chirag Gangdev
0 votes

Try this method:

#include< stdio.h>

    int main()
    {
    char str[10] ;
    int i=0,j;
    printf("Enter a String:\n");
    scanf("%s",&str);
    while (str[i]!='\0')
    {
    i++;    
    }
  str[i++]='\0';
    printf("Reversed String is:\n");    
    for(j=i-1; j>=0;j--)
    {
    printf("%c",str[j]);
    }
answer Feb 7, 2016 by Shivam Kumar Pandey
...