0

I've a little problem with my code, why when I use printf on string1 (last line), it doesn't give me what I wrote for this variable ?

For example if I wrote : asdfgh, string1 give me something weird like : @>>..

Any idea ?

Thanks for help.

 int main()
{
    int length;
    int i = 0;
    char string1[100];
    printf("Please enter the length of the two strings\n");
    scanf("%d", &length);

    printf("\nPlease enter the first string\n");
    while((string1[i] = getchar())!='\n')
        i++ ;
    getchar();
    printf("\nString 1 : %c", string1);

    return 0;
}
4
  • did you mean %s in printf("\nString 1 : %s", string1); also don't forget to put null-terminattor. Commented May 12, 2014 at 21:06
  • There are several things wrong with this code, but the most immediate is that your string is not null terminated, and you use %s to printf() strings, not %c. Commented May 12, 2014 at 21:06
  • Thx, I modified the %, and I asked for help for terminate the string. It's the first I use getchar.. It's a little bit problematic for me :s Commented May 12, 2014 at 21:16
  • Another major problem is that the while loop reads the remainder of the first line, where the user typed in the length and pressed enter. Anything the user types after the second prompt appears will not be read. Commented May 15, 2014 at 4:14

2 Answers 2

1

You have few problems there:

1) Should use %s for printing string.
2) Terminate the string with NULL terminator (It's not a string until then ;)
3) use a standard prototype for main(), such as: int main(void)

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

9 Comments

Hey! Thx for your answer, you re right. Null terminator is '\0' yeah ? Where can I find help with this ? Because in this case, I don't know how to terminate the string.. Thx.
Yes '\0' or just 0 will do. After the loop add: string1[i]='\0'; Remember you can only input 99 chars (+1 for NULL). Otherwise, you'll run into more problems.
Ah! Yes of course, I wrote string1[100]='\0' and not i ! (My mistake). Now my code is : int main(void) { int length; int i = 0; char string1[101]; printf("Please enter the length of the two strings\n"); scanf("%d", &length); printf("\nPlease enter the first string\n"); while((string1[i] = getchar())!='\n') i++ ; string1[i] = '\0'; getchar(); printf("\nString 1 : %s", string1); return 0; }
I dont think you should have the last getchar() after you have put in the NULL terminator, I guess it confuses the input buffer.
Ok, there's another problem. You need use another getchar() right after the scanf() call to consume the \n left, which makes the while loop condition fail right away without entering the loop at all. TBH, I don't get the point reading length since you don't use it anywhere. Besides, scanf() is not a friendly input function, on which you should read about more. Here's one: c-faq.com/stdio/scanfprobs.html
|
0
 #include <stdio.h>
 int main(void)
 {
    int length;
    int i = 0;
    int ch;
    char string1[100];
    printf("Please enter the length of the two strings\n");
    scanf("%d", &length);
    getchar();
    printf("\nPlease enter the first string\n");
    /* use null for termination of string */
    /* Press Ctrl+d to end your input */
    while((ch = getchar()) != EOF){
            string1[i++] = ch;

    }
    string1[i] = '\0';
    /* USE %s to print whole string */
    printf("\nString 1 : %s\n", string1);

    return 0;
  }

Hope this will solve your problem

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.