Program in C Ask for a phrase and count how many "LA" words are in it using string functions. HELP
int i,n=0; char phrase[N], word[N]="la", aux[N]; printf("\n write a phrase..."); gets(phrase); strcpy(aux,phrase); strlen(aux); for (i=0;i<strlen(aux);i++){ if (strstr(aux,word) != NULL){ strcpy(aux,strstr(phrase,word)); n++; } else printf("\n None"); } printf("\n Found %s %d ",word,n); return 0;
I tried this, but it aint working properly, i think the issue is in the for loop
ok well first why are you using "chars" when you want a string? Do you know about searching for letters within a string? In Java we have contains(CharSequence s) Returns true if and only if this string contains the specified sequence of char values. next what is gets, strcpy and strlen? getting something, copying something, and length of something?
yeah well is pure C im not using JAVA and yes you are right with the functions
What I want to know is, you have arrays and then use the singlular of that array, is that even allowed? I.E., aux[N] and aux... I don't think you can do that, but I'm not 100% on C....
yup, you can , you are just doing references
so it references the entire array then? ic....
uhum :)
now why exactly do we need both phrase and aux? It seems that we have our word "LA" and all we need is to compare it to another? Are you trying to take out one of the words while it searches for the others?
yeah we might dont need that right?
gets cant be used with aux tho thats why i copy the char into the aux with the function
I thought they are both Chars? Weird...
yeah well i guess i give up :(
noo :( .... I wish I could be more of help, though I haven't taken a C class in 6-7 years or so? :(
It's okay :( I'll talk to my prof so i wont get points out T>T
char* found = &phrase; while( ( found = strstr( phrase, word ) ) != NULL ) { n++; found ++; }
Join our real-time social learning platform and learn together with your friends!