I am new to C and I am trying to write a program that takes an entered name, like john smith, and returns the uppercase initials, JS. I've tried using a for loop and a while loop, but my code does not seem to increment, whenever I run it all it returns is the first initial. I've searched the web for this problem, but none of the solutions worked for me. What am I doing wrong? Thanks in advance.
Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
int main(void) {
// initialize variables
char name[61];
int i = 0;
// ask for user input
printf("Please enter your name: ");
scanf("%s", name);
// print first initial
printf("%c", toupper(name[0]));
// print the next ones
while (name[i] != '\0') {
if (name[i] == ' ') {
i++;
printf("%c", toupper(name[i+1]));
}
i++; // does not increment
}
return 0;
}