0

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.

1 Answer 1

1

Once set to true, you never reset your variables to false.

You could just if (isspace(nms[pos]) && isalpha(nms[let])), or use like cond1 = isspace(nms[pos]);.

Also, I think it should be for (pos=0;pos<strlen(nms)-1;pos++). Imagine the string " M C Hammer ", with your loop, it won't print the M.

2
  • Now it makes sense! Thank you! I went with if (isspace(nms[pos]) && isalpha(nms[let])) for the sleeker design. Since I thought maybe someone would start with spaces, I had the program print the first character to start with, if it's an alpha.
    – contranull
    Nov 15, 2017 at 15:39
  • You could also have like for (pos=0; pos < strlen(nms); pos++) { if ((pos == 0 || isspace(nms[pos - 1])) && isalpha(nms[pos])) { putchar(toupper(nms[pos])); }} without the need for testing nms[0] separately. If pos == 0, the nms[pos-1] part isn't even evaluated as || short-circuits.
    – Blauelf
    Nov 15, 2017 at 16:23

You must log in to answer this question.

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