Ahmed Salah El Din Ahmed Salah El Din - 1 month ago 6
C Question

Splitting strings and printing the capitalized first characers in C

I am working on a code that takes the string containing the name of someone and prints the initials of that name capitalized, whenever I run my code, I keep getting the initials printed twice, which I don't know how to fix this issue and get the desired output.

Here is my code:

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main(void)
string name = GetString();
char* pointer;
pointer = strtok(name, " ");
while (pointer != NULL)
printf("%c", putchar(toupper(pointer[0])));
pointer = strtok (NULL, " ");

when I run the code with for example: ahmed salah eldin



I just need :



You are using printf() and putchar() you just need one of them. When you call putchar() it returns the output character which is then passed to printf() and is output again.

Change that to

fputc(toupper(pointer[0]), stdout);