简体   繁体   中英

string reverse program in C

i have written a program to reverse a string.. But it is not working.. It is printing the same string which is scanned.. What is the problem with the code?

#include <stdio.h>
#include <stdlib.h>
char *strrev(char *s)
{
        char *temp = s;
        char *result = s;
        char t;
        int l = 0, i;
        while (*temp) {
                l++;
                temp++;
        }
        temp--;
        for (i = 0; i < l; i++) {
                t = *temp;
                *temp = *s;
                *s = t;
                s++;
                temp--;
        }
        return result;
}

int main()
{
        char *str;
        str = malloc(50);
        printf("Enter a string: ");
        scanf("%s", str);
        printf("%s\n\n", strrev(str));
        return 0;
}
for (i = 0; i < l; i++)

You're walking through the entire string, so you're reversing it twice - it won't be reversed after all. Walk only halfways:

for (i = 0; i < l / 2; i++)

Also, try using int len = strlen() instead of the while-not-end-of-string loop, if you're permitted to do so.

您交换字符串的内容两次。

Use the following code ..

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>

char *strrev(char *s)
{
     char *temp = s;
     char *result = s;
     char t;
     while (*temp)
          temp++;

     while (--temp != s)
     {
            t = *temp;
            *temp = *s;
            *s++ = t;
     }
     return result;
 }

 int main()
 {
      char *str;
      str = (char*)malloc(50);
      printf("Enter a string: ");
      scanf("%s", str);
      printf("%s\n\n", strrev(str));
      return 0;
  }

The logic is to swap characters from start upto first half with the characters from last of second half, ie, upto len/2. Just modify your for loop as below & it will work fine for you for (i = 0; i < l/2; i++) {

you can use this simple code

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>


int str_len (char *str)
{
   char *ptr = str;
    while (*str)
     str++;
   return str - ptr;
}

int main ()
{
  char *str;
  int length;
  str = (char*)malloc(50);
  printf("Enter a string: ");
  scanf("%s", str);
  length = str_len(str) - 1;

  for (int i = length ; i >= 0 ; i--)
  printf ("%c", str[i]);
  return 0;
}

Actually you are reversing the string twice...so after come to middle of the string, you should terminate the loop that is your loop should be run for half of the string length that is l/2 (in this case). so your loop should be like

for(i = 0; i < i / 2; i++)

swapping the string content twice..

swapping it once will help..

for (i = 0; i < l/2; i++)
you can use this code to reverse the string
#include<stdio.h>
#include<string.h>
int main()
{
    int n,i;
    char str2[100],str1[100];
    printf("enter teh string 1\n");
    gets(str1);
    n = strlen(str1);
    for(i=0;i<n;i++)
    {
    str2[n-1-i]=str1[i];
    }
    printf("%s\n",str2);

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM