Sunday, 8 September 2013

Print A String All In Uppercase

I like the for(x=0;(char1[x]=getchar())!='\n';x++); line. That's why I'm posting it.

This program is from Programming With C by Byron S. Gottfried.
It displays a user entered string in uppercase.
for(x=0;(char1[x]=getchar())!='\n';x++); accepts the user entered characters saving it in char1[0],[1],[2] and so on until the user hits enter ('\n').

Then the string is displayed character by character. Each character is 'upper-cased', so to speak, by the toupper() function before it is displayed.

#include <stdio.h>

main()

{
    char char1[100];
    int x,y;

    printf("Enter a length of string: ");

    for(x=0;(char1[x]=getchar())!='\n';x++);

    y=x;

    
    for(x=0;x<y;x++)
    {
        putchar(toupper(char1[x]));
    }

}

No comments:

Post a Comment