1

I am trying to reverse a string. scanf is working well but when I use fixed string then it gives garbage value. So where is the fault ?

#include<stdio.h>
#include<string.h>
int main()
{
    char s[50]="Hi I Love Programming";
    char rev[strlen(s)];
    int i,k;



    k=strlen(s);
    for(i=0; i<strlen(s); i++)
    {
        rev[k]=s[i];
    k--;
    }

    printf("The reverse string is: %s\n", rev);
}

enter image description here

2 Answers2

5

Your program has two issues:

1.

char rev[strlen(s)];

You forgot to add an element for the string-terminating null character '\0'.

Use:

char rev[strlen(s) + 1];

Furthermore you also forgot to append this character at the end of the reversed string.

Use:

size_t len = strlen(s);
rev[len] = '\0';

Note, my len is the k in your provided code. I use the identifier len because it is more obvious what the intention of that object is. You can use strlen(s) because the string has the same length, doesn´t matter if it is in proper or reversed direction.


2.

k=strlen(s);

for(i=0; i<strlen(s); i++)
{
    rev[k]=s[i];
    k--;
}

With rev[k] you accessing memory beyond the array rev, since index counting starts at 0, not 1. Thus, the behavior is undefined.

k needs to be strlen(s) - 1.


Three things to note:

  1. The return value of strlen() is of type size_t, so an object of type size_t is appropriate to store the string length, not int.

  2. It is more efficient to rather calculate the string length once, not at each condition test. Use a second object to store the string length and use this object in the condition of the for loop, like i < len2.

  3. char s[50]="Hi I Love Programming"; can be simplified to char s[]="Hi I Love Programming"; - The compiler automatically detects the amount of elements needed to store the string + the terminating null character. This safes unnecessary memory space, but also ensures that the allocated space is sufficient to hold the string with the null character.


The code can also be simplified (Online example):

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

int main(void)
{
    char s[] = "Hi I Love Programming";
    size_t len = strlen(s);

    char rev[len + 1];
    size_t i,j;

    for(i = 0, j = (len - 1); i < len; i++, j--)
    {
        rev[j] = s[i];
    }

    rev[len] = '\0';

    printf("The reverse string is: %s\n", rev);
}

Output:

The reverse string is: pgnimmargorP evoL I iH
Community
  • 1
  • 1
1

your program is hard to understand. Here you have something much simpler (if you want to reverse the string of course)

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

char *revstr(char *str)
{
    char *start = str;
    char *end;

    if(str && *str)
    {
        end = str + strlen(str) - 1;
        while(start < end)
        {
            char tmp = *end;
            *end-- = *start;
            *start++ = tmp;
        }
    }
    return str;
}


int main()
{
    char s[50]="Hi I Love Programming";

    printf("%s", revstr(s));
}

https://godbolt.org/z/5KX3kP

0___________
  • 60,014
  • 4
  • 34
  • 74