Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

How to print words which end on -aty?

+2 votes
asked Dec 20, 2020 by Miafina (930 points)
edited Dec 20, 2020 by Miafina
Hello!

I tried to count the number of words which end with aty. I don't know how to print them. pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysoki baty means congratulate on the new year and wish to hand in all the works in time.
I used the strtok function.
The program doesn't work correctly. There are four needed words but the program prints only the first one for 4 times.

#include <stdio.h>
#include <string.h>
int main()
{
char text[]=" pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysiki baly ";
char *word=text;
char *result;

int n=0;
printf("pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysoki baly");
printf("\nСлова, які закінчуються буквосполученням 'aty':");
while(*word){
    
    if(*word!=' '){
       if(*word=='a'){
       word++;
       if(*word=='t'){
           word++;
           if(*word=='y'){
               word++;
               if(*word==' '){
                   word++;
                   n++;
                   result=strtok(text," ");
                   printf("%s\n", result);
                   
               }
           }
       }
    }
    }
       word++;
    }

printf("\nЇх кількість: %i",n);
    
return 0;
}

Do you have any idea?

Thank you in advance)

1 Answer

+1 vote
answered Dec 21, 2020 by xDELLx (7,630 points)
#include <stdio.h>
#include <string.h>
int main()
{
char text[]=" pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysiki baly ";
char *word=text;
char *result;

int n=0;
printf("pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysoki baly");
printf("\nСлова, які закінчуються буквосполученням 'aty':");
/*
strtok the string to find space
check the last 3 letter,if they match to a,t,y
if yes print word
if no continue loop

*/printf("\n");
result = strtok(word," ");
while(result!= NULL){
    //result has the first token??
    //    printf("String is ---> %s\n",result);    
    //count od string >=3 ?? ie it has aty as its last char ??
    int len=strlen(result);
    if (len >2 &&
         (result[len-1]=='y' &&
            result[len-2]=='t' &&
            result[len-3]=='a')
        ){
        n++;
        printf("String is ---> %s\n",result);    
    }
    //parse the string further
    result = strtok(NULL," ");
}

printf("\n");
printf("\nЇх кількість: %i\n\n",n);
    
return 0;
}
/*
pryvitaty z novym rokom i pobazhaty zdaty vsi roboty i otrymaty vysoki baly
Слова, які закінчуються буквосполученням 'aty':
String is ---> pryvitaty
String is ---> pobazhaty
String is ---> zdaty
String is ---> otrymaty


Їх кількість: 4
*/

Man page for strtok (on linux ) has good sample prog & explanation of how strtok works.

commented Dec 21, 2020 by Miafina (930 points)
Thank you! Now I understand it much better.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...