1

Here is the code

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
  clrscr();
  char a[20],rev[20];
  printf("enter the string");
  scanf("%s",a);
  int len=strlen(a);
  for(int i=0;i<len;i++)
  {
    rev[i]+=a[len-i-1];
  }
  printf("%d \t  \n string is \t %s",len,rev);
  getch();
}

It was correctly working when we gave it a string without spaces:

input: welcome
len:7
output: emoclew

When we give it a string with a space:

input : welcome to this world
len:7
output:some other ascii chars that I have not seen so far. and the "len" is again 7 only

When I change the following statement:

scanf("%s",a) to gets(a);

I get:

input :welcome to this world
len:21
output : something different. not the reverse of string...

In this case "len" is correct but the output is wrong.

What is really happening? What is the problem with the above code?

Lance Roberts
  • 22,383
  • 32
  • 112
  • 130
thilak
  • 11
  • 1
  • 1
    either don't input a string longer than 20 chars or increase your buffer size (char a[20],rev[20];). – Tugrul Ates Jun 26 '11 at 08:48
  • 1
    don't use `scanf("%s",a)`. You cannot know how long the input is, so you must use a function that takes the buffer size as argument. – CodesInChaos Jun 26 '11 at 08:52
  • 1
    Do not use `gets`, it will let you input as many characters as you want, over-run your buffer, and potentially crash your program – jonsca Jun 26 '11 at 08:53
  • scanf and gets are considered unsafe functions. You can check [here](http://stackoverflow.com/questions/2977553/if-one-complains-about-gets-why-not-do-the-same-with-scanfs) – dave Jun 26 '11 at 08:53
  • We have a whole choir going here :) – jonsca Jun 26 '11 at 08:54
  • @jonsca, yeh, that was a crazy poor formatted post. I'm done with it, go wild. – Lance Roberts Jun 26 '11 at 08:56
  • i have used string length below 20.. but the output is same.. – thilak Jun 26 '11 at 08:57
  • @Lance yes, that was fun. Why would you use more than one question mark???? Why??? I don't understand....... – jonsca Jun 26 '11 at 08:58
  • 1
    @jonsca, yeh, I missed those, took them out of the title, but forgot them in the text. I spent most of my time with the quoted result stuff, the quote button has real limits in the editor. I'll jump back in and take care of it. – Lance Roberts Jun 26 '11 at 08:59
  • @Lance style....................k what can i do to solve this pbm?? – thilak Jun 26 '11 at 09:00
  • @thilak, indent your code where appropriate, you can use the quote button for those output excerpts like I did, though they sometimes take a little work. – Lance Roberts Jun 26 '11 at 09:02
  • @black: `scanf` is not insecure, you can pass it a maximum width specifier. – ninjalj Jun 26 '11 at 09:03
  • 1
    @jonsca, thanks for the question mark fixed, I found some other grammar things and patched it up. – Lance Roberts Jun 26 '11 at 09:03
  • @Lance Roberts oh fine lance. can u provide me a solution to get out this pbm?... – thilak Jun 26 '11 at 09:04
  • If you want decent user input, see http://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921 : `scanf("%s")` and `gets()` are just asking for trouble. – paxdiablo Jun 26 '11 at 09:07

4 Answers4

5

scanf will not read the entire line. Instead it'll read up to the first space... You need getline

Also, I notice you have things with length more than 19 but you allocated space with for 20 chars. Increase that or you get UB

Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • @Armen Tsirunyan when i use getline method it issues me an linked error. i also gave the input string less than 20 but the output is still not correct...(that is the string is not correctly reversed") – thilak Jun 26 '11 at 08:58
  • 3
    `scanf` can read an entire line if you use a scanset conversion (`%19[^\n]`) instead of `%s`. `getline` is not part of the standard C library. For portable code, either the scanset conversion or `fgets` is reasonable. – Jerry Coffin Jun 26 '11 at 09:04
  • `getline` is not standard C - you can find a solution similar to that (without the powerful line editing) at http://stackoverflow.com/questions/4023895/how-to-read-string-entered-by-user-in-c/4023921#4023921 – paxdiablo Jun 26 '11 at 09:08
  • the %[ stuff is a nice thing I often tend to forget... about getline, it is in POSIX.1-2008, but I guess the use of conio means the user likely is not on a posix system... – ShinTakezou Jun 26 '11 at 15:32
3

In addition to what others have said, rev is not guaranteed to be initialized to NUL characters, so your rev[i]+=a[len-i-1]; line can end up with garbage.

ninjalj
  • 42,493
  • 9
  • 106
  • 148
2

Use the following:

scanf("%[^\t\n]",string);

letsc
  • 2,075
  • 5
  • 24
  • 43
1

To comments on it:

  1. As Armen Tsirunyan wrote, you probably want getline
  2. You only have a character array of size 20 - so passing a bigger string will occur in a buffer overflow (which makes your program very insecure - google for buffer overflow attack if you want to know more about it) - this is why you should make sure that you never read more than the size of your character array...
Markus Pilman
  • 3,104
  • 3
  • 22
  • 31