0
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
{
    printf("Enter full name: ");
    string s = get_string();

    if(s != NULL)
    {
        for(int i = 0, n = strlen(s); n > i; i++)
        {
            printf("%c", toupper(s[0]));
        }


    }
}
1

Please delete if this is the code you are trying. I don't ubderstand the use while in your code as well as you are printing integer instead of the characters. (This will print their ASCII values)

Suggestion:

  • input string

  • iterate over each of its character till "\0".

  • print first character as printf("%c", toupper(input[0]);

  • chech for character space " ", and print its next character as initial.

  • Revised my code and now im running into an infinite loop. It prints the first letter of any name in CAP but does so in an infinite loop. Any suggestions? – Akil M Hylton Jul 13 '17 at 23:39
  • Check for the condition in the for loop, should be n < i. – MIB Jul 14 '17 at 2:22
  • @MIB "Check for the condition in the for loop, should be n < i". No, it shouldn't. – ChrisG Jul 14 '17 at 9:58
  • Oops, you are right @ChisG. i usually right as i < n nad thus misinterpreted it. Thanks – MIB Jul 14 '17 at 10:04
0

Inside your for loop, you always print the first letter in your string (s[0]). You should probably use the variable i, which is also controlling your loop. So if the i-th character is a space, you print the (i+1)-th character in uppercase, as an initial.

You also incorrectly mention that your program enters an infinite loop. An infinite loop would mean that your program never terminates on its own and you would have to kill it by pressing Ctrl+C. Instead, your program just prints the same character as many times as the length of the string, and then it terminates on its own. There is a big deference there.

You should also probably re-watch the walkthroughs.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .