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?

15
  • 1
    either don't input a string longer than 20 chars or increase your buffer size (char a[20],rev[20];). Commented Jun 26, 2011 at 8: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. Commented Jun 26, 2011 at 8: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 Commented Jun 26, 2011 at 8:53
  • 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. Commented Jun 26, 2011 at 8:59
  • 1
    @jonsca, thanks for the question mark fixed, I found some other grammar things and patched it up. Commented Jun 26, 2011 at 9:03

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

4 Comments

@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")
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.
getline is not standard C - you can find a solution similar to that (without the powerful line editing) at stackoverflow.com/questions/4023895/…
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...
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.

Comments

2

Use the following:

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

2 Comments

And that still has the buffer overflow issue :-)
@pax: I think the OP is a noob to C. So IMAO we shouldnt petrify him with ugly-looking words like buffer overflow and stuff... ;)
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...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.