3

What's the problem with the following code?

#define N 30
int main() {
    char str[N], new_str[N];
    int i,len;
    printf("Please enter 20 letters. \n");
    scanf("%s", str);
    len = strlen(str);
    printf("The length of str is  %d ", len);
    for (i=0; i< len; i++)
        new_str[i]=str[len-1-i];
    printf("The result is: %s\n", new_str);
    return 1;
}

I checked that for every string under 16 characters the program is OK, and above it returns undefined characters at the end:

  Please enter 20 letters.
  1234567891111111

  The result is: 1111111987654321q=V?.

However, if I initialize str, new_str with "" the problem is solved. I still I wonder what causes the problem.

1
  • 1
    You have to add the \0 character for the new_str like new_str[i]= '\0' after the for loop completes. Commented Jul 18, 2012 at 9:37

4 Answers 4

10

You need to add the null terminator:

new_str[len] = '\0';

otherwise, there is garbage after the last character, so the string is not ending

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

13 Comments

Isn't it being add automatically when I enter a string?
to the first string, but strlen returns the length without the null terminator.
@Numerator: At new_str? Why should it? You are not inputting it, you are inputting str.
@dda: and even if it were included, reversing would put the terminator to the beginning. :-)
Note that the \0 is added by many string functions too; strcpy, strcat and so on. And with constant assignments like const char* s = "string"; But not when manipulating the individual chars in the string manually!
|
9

You need to add a null terminator to your new_str or printf doesn't know when to stop. Alternatively you could reverse the string in situ, like this.

char s[] = "StackOverflow";
size_t l = strlen(s);
int i, j, k;
k = l/2;
// Work with half the length
for (i=0; i<k; i++) {
  // swap the two bytes
  j=s[i];
  s[i]=s[l-1-i];
  s[l-1-i]=j;
}

8 Comments

Thanks guys :-) I wanted the fastest reverse possible. But this code would break like glass in a kindergarten juggling contest if you used it on 2-byte+ encodings :-)
@dda: well, so does the copying variant of reversing as well. Indeed, utf-8 and the like are a completely different story: such "strings" shouldn't be called "strings" and should IMHO be processed only with a special library (maybe self-made), manual processing is too error-prone. (Perhaps someone knows such a library for C?)
@Vlad ehm, why should UTF-8 encoded strings not be called strings?
@Vlad For serious business purposes, IBM's ICU library is what you need. Maybe a bit of overkill for simple stuff like this though.
I agree with @Vlad. UTF-8 strings are byte arrays where each element is of variable length. To get the next char, you need to look at each byte and depending on its value add it to the pile, or keep going.
|
2

Strings should be zero-terminated. After the scanf, str is, but new_str hasn't been given a value, so it still contains garbage. After the loop, the first (20?) characters have been set, but you still need to append a \0.

Comments

1

new_str is not NUL terminated.

After you have copied all the characters from str, you need to add a '\0' to the end of new_str like this:

new_str[i] = '\0';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.