How to count the number of words in a string that end with s or y in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>
  
int CountWordsEndingWithSOrY(char str[], char* delimiter) {
    char* word = strtok(str, delimiter);
    int count = 0;
  
    while (word != NULL) {
        if (word[strlen(word) - 1] == 's' || word[strlen(word) - 1] == 'y') {
            count++;
        }
        word = strtok(NULL, delimiter);
    }
    
    return count;
}
  
int main(void)
{
    char s[] = "c lens c++ news java enjoy chess c# money";
  
    printf("%d", CountWordsEndingWithSOrY(s, " "));
  
    return 0;
}
  
  
  
  
  
/*
run:
  
5
  
*/

 



answered Jan 5, 2024 by avibootz
...