How to get first letter of each word in a string with C

2 Answers

0 votes
#include <stdio.h>
#include <string.h>

void print_first_letter(char str[], char* delimiter) {
    char* word = strtok(str, delimiter);

    while (word != NULL) {
        printf("%c\n", word[0]);
        word = strtok(NULL, delimiter);
    }
}


int main(void)
{
    char s[] = "C is a general purpose procedural computer programming language";

    print_first_letter(s, " ");

    return 0;
}





/*
run:

C
i
a
g
p
p
c
p
l

*/

 



answered Nov 10, 2022 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

void print_first_letter(char str[], char delimiter) {
    for (int i = 0; i < strlen(str); i++) {
        if (i == 0 && str[i] != delimiter) {
            printf("%c\n", str[i]);
        }
        else if (i > 0 && str[i - 1] == delimiter) {
            printf("%c\n", str[i]);
        }
    }
}


int main(void)
{
    char s[] = "C is a general purpose procedural computer programming language";

    print_first_letter(s, ' ');

    return 0;
}





/*
run:

C
i
a
g
p
p
c
p
l

*/

 



answered Nov 10, 2022 by avibootz
...