The "more comfortable" version seemed most interesting, so I gave it a shot.
//declare the string "nms" and prompt for string input
printf("What is your name?\n");
string nms=get_string();
//if the first character is a letter
if (isalpha(nms[0]))
{
//capitalize it and print it
printf("%c",toupper(nms[0]));
}
//declare the boolean cond1 for condition 1
bool cond1=false;
//declare the boolean cond2 for condition 2
bool cond2=false;
//declare the integer pos, which will signify a position in the nms string
int pos=1;
//Starting from the second position, check every position in nms for conditions 1 and 2, below
for (pos=1;pos<strlen(nms);pos++)
{
//declare the integer let = pos +1
int let=pos+1;
//condition 1: the current position of symb must be blank (a space or tab)
//information about isblank and isalpha can be found here: https://reference.cs50.net/ctype/isblank
if isspace(nms[pos]) cond1=true;
//condition 2: the curremt position of let must be an alphabetic character (a letter)
if isalpha(nms[let]) cond2=true;
// if conditions 1 and 2 are true
if (cond1==true && cond2==true)
{
//capitalize and print the character in the let position
printf("%c",toupper(nms[let]));
}
}
I'm only getting the first and third characters of the input, where the third is printed 12 times. Here's the output:
~/workspace/pset2_test/ $ make initials_space
clang -fsanitize=signed-integer-overflow -fsanitize=undefined -ggdb3 -O0 -std=c11 -Wall -Werror -Wextra -Wno-sign-compare -Wshadow initials_space.c -lcrypt -lcs50 -lm -o initials_space
~/workspace/pset2_test/ $ ./initials_space
What is your name?
Armor Moler Rover
AMOLER ROVER
I added "return 0;" after the last printf, and got the result AM
, so the program understandably stopped. How do I make it start at the beginning of the "for" loop again? I thought that would happen automatically as long as pos was less than the string length.
I am at a loss. Please help!
Edit: it just prints out everything, even the spaces, after the first space.