I felt the answers here do not really leverage the C library much so I decided to show another way. If you're new to C, I really recommend going through the code and reading the man page (type man [command] in terminal and read the docs) for each of the functions here that you are not familiar with. Specifically, look at my use of sprintf() in the for loop. Understanding why that works is a huge step forward in C. I also recommend reading the seminal book The C Programming Language by Kernighan and Ritchie. You will not only only become a better C programmer, but a better programmer in general.
words is an array of strings where each string is a word in the name. In your example, the array would be:
words[0] = "Edward"
words[1] = "Cantrell"
words[2] = "Cavender"
words[3] = "Davis\n"
*note the new line character at the end of the last word. getline() returns the raw user input, and since user pressed enter to signify the end of input, the new line character carries through.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define LEN 200
void name_abbreviated(char *name) {
char tmp[LEN+1], abbr[LEN], *words[LEN], *token;
int i = 0;
strncpy(tmp, name, LEN);
token = strtok(tmp, " ");
while (token) {
words[i++] = token;
token = strtok(NULL, " ");
}
// remove '\n' from last word
words[i-1][strlen(words[i-1]) - 1] = '\0';
sprintf(abbr, "%s, ", words[i-1]);
for (int j = 0; j < i - 1; j++)
sprintf(abbr + strlen(abbr), "%c. ", words[j][0]);
puts(abbr);
}
int main(void) {
char *name = NULL;
char *abbr;
size_t linecap = LEN;
printf("Type a full name : ");
getline(&name, &linecap, stdin);
name_abbreviated(name);
}