0

I originally used a for loop (for(int i = 0, n =strlen(name); i < n ; i++), but decided to try a while loop setting i=1. Either way my output is always limited to the first character.

I've tried tweaking various small aspects to the design, but nothing is working. I'm not sure if there's a syntactical or logical error I'm not seeing. Any suggestions?

I've attached both sets of code.

int main(void)
{
//prompt user for name input as get_string 
//printf("Input Name: "); 
string name = get_string(); 

//check if array is empty 
if (name != NULL)
{
    //print the first character 
    printf("%c", toupper(name[0]));
    //index through the rest of the characters in the array 
    int i =1 ; 
    int n = strlen(name); 
    while (i <= 1 && i<=n)
    {
        //check if the element is a space and next element is a null terminator 
        //if element is a space and next is a char --> print the char 
        if (isspace(name[i]) == true && name[i+1] != '\0')
        {
            printf("%c", toupper(name[i+1]));
            //i++;
        }
    i++; 
    }
}
printf("\n");
}

This was my original thought process - the use of a for loop. Is it redundant to have i++ at the end of this loop after I've specified to iterate over again in the for loop?

int main(void)
{
//prompt user for name input as get_string 
//printf("Input Name: "); 
string name = get_string(); 

//check if array is empty 
if (name != NULL)
{
    //print the first character 
    printf("%c", toupper(name[0]));
    //index through the rest of the characters in the array 

    for (int i =0, n = strlen(name) ; i < n ; i++)
    {
        //check if the element is a space and next element is a null terminator 
        //if element is a space and next is a char --> print the char 
        if (isspace(name[i]) == true && name[i+1] != '\0')
        {
            printf("%c", toupper(name[i+1]));
            //i++;
        }
        i++; 
    }
}
printf("\n");
}

2 Answers 2

1

Problem with the while version here: while (i <= 1 && i<=n). It will execute the loop exactly once. After i++, i is 2, and i <= 1 is false, loop over. (I think there's a problem with i<=n too, since that will test an invalid index here name[i+1] != '\0')

As for the for loop, I wouldn't say redundant exactly. Since it is incrementing i twice each time the loop executes, it's incorrect. It will only test even indexes. (And beware, if the loop starts at index 0, it will print the first "initial" twice).

1
  • Thanks so much! I definitely got sloppy while writing the "while" loop. I eliminated the second incrementation of i and made a change mentioned above and it worked perfectly.
    – mattiek
    Commented Jun 14, 2017 at 17:32
1

The previous answer points you some mistakes, you should correct them. But your major conceptual problem is here:

if (isspace(name[i]) == true && .....)

it does not work like that. isspace() by itself return true or false, you do NOT need to compare it with true or false. It is the same mess as if you would do like that:

if((n > 1) == true)

meanwhile the correct code is:

if (n > 1)

so, try to use just:

if(isspace(name[i]) && ..... )
1
  • Thank you so much! That in addition to cleaning up the doubling of incrementing i fixed everything!
    – mattiek
    Commented Jun 14, 2017 at 17:30

You must log in to answer this question.

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