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");
}