0

So here I'm trying to enter a string and reverse it manually in C. If I just use a number let's say 5 as the length of the string directly everywhere (while creating the character array, in for loops) then the code works fine.

But if I take a variable to store the length and take user input for the length, let's say we enter 5, then I get an output like this

enter length : 5

enter string of length 5 : apple

the string is :

appl

rev string is : lppa

Why am I missing the last character

If I just assign value to the variable during declaration i.e. int len = 5; then the code works fine too

The issue is only when I take user input for len variable

Here is my code

include <stdio.h>
int main()
{
int len;
printf("enter length : ");
scanf("%d", &len);
int i, j = len-1;
char str[len], rev[len];

printf("enter string of length %d : ", len);
for (i = 0; i < len; i++)
{
    scanf("%c", &str[i]);
    rev[j] = str[i];
    j--;
}

i = 0;
printf("the string is : ");
for (i = 0; i < len; i++)
    printf("%c", str[i]);

j = 0;
printf("\nrev string is : ");
for (j = 0; j < len; j++)
    printf("%c", rev[j]);
return 0;
}
madlading
  • 9
  • 2
  • `char str[len]` is a bit malformed outside of using VLAs. You likely wanted to either allocate memory dynamically (with `malloc`), or wanted a constant length. Note as well, c-strings need to be terminated with a null char (`'\0'` or `0` or `NUL`). – Rogue Nov 10 '22 at 18:19
  • 7
    There is a newline remaining in the buffer after the first `scanf`, which is being read as the first character of the next input. If you use `scanf(" %c", &str[i]);` with that added space the program should "work" but the input cannot contain whitespace characters (they will be filtered). Please see [scanf() leaves the newline char in the buffer](https://stackoverflow.com/questions/5240789/scanf-leaves-the-new-line-char-in-the-buffer). – Weather Vane Nov 10 '22 at 18:25
  • you asked an almost identical question a few hours ago – 0___________ Nov 10 '22 at 18:30

1 Answers1

0

need to iterate loop into till null char. because string always terminated with null char.

just added minor fix into your code.

for (i = 0; i < len+1; i++)

in order to reverse the string, below modification is required.

  // set to j to end of string.copy into rev array from str arr
  j = len ; 
  printf ("\nrev string is : ");
  for (i = 0; i <= len; i++)
  {
        rev[i] = str[j];
        j--;
        printf ("%c", rev[i]);
  }

Please check my comment into below

#include <stdio.h>

int main ()
{
  int len;
  printf ("enter length : ");
  scanf ("%d", &len);
  int i, j = len - 1;
  char str[len], rev[len];

  printf ("enter string of length %d : ", len);
  for (i = 0; i < len+1; i++)
    {
      scanf ("%c", &str[i]);
      rev[j] = str[i];
      j--;
    }

  i = 0;
  printf ("the string is : ");
  for (i = 0; i < len+1; i++)
    printf ("%c", str[i]);

  j = len;
  printf ("\nrev string is : ");
  for (i = 0; i <= len; i++)
    {
      rev[i] = str[j];
      j--;
      printf ("%c", rev[i]);
    }
  return 0;
}

output of code

enter length : 5
 length is 5 
enter string of length 5 : apple
the string is : 
apple
rev string is : elppa
user8811698
  • 118
  • 1
  • 7