I have a problem of running this code. when i try to complie, it keeps repeating on the same warnings. Here's my code. Here's the warning: initials.c:14:12: error: expression result unused [- Can somebody help please? I have stuck on it for few days. pset2 is so hard!
2 Answers
Look at these two lines again:
char c = toupper(name[0]);
printf("%c", toupper(name[0]);
You're basically doing the same thing twice, so your variable 'c' is unused (just print 'c', no need to toupper again). Also, your printf statement is incorrect. You don't need the ':' in there. If you want ':' to be printed, you need to include it inside the brackets and quotations that go inside the printf's brackets.
Look at these lines :-
for(i = 0; i < strlen(name); i++)
{
if (name[i - 1] == ' ')
{
printf:("%c\n", toupper(name[i]));
}
}
Here, you have i = 0, but you are referencing to the value of name[i - 1] , which is outside the bounds of the string.
Also, as iCode95 has also mentioned , you don't have to put the colons outside the printf()'s .
Here, you are also printing a new line after every capitalized initial letter, as it is in a loop.But you have to print the new line after all the initials in the name.So, better have a new line after the for loop.
printf("%c\n", toupper(name[i]));
Also, i would suggest you to call strlen(name) only once, as it's never gonna change. So instead of keeping it in the condition part of the for loop, you can keep it either above the for loop or in its initialization part(as I have done). Just a better design pattern.
So, in a nutshell, you can do something like this.
for (i = 0, n = strlen(name); i < n; i++)
{
if (name[i == ' '])
{
printf("%c", toupper(name[i + 1]))
}
}
printf("\n");