1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
#include <stdio.h>
#include <string.h>
#include <ctype.h>
unsigned const_count(char* string){
unsigned const_count =0 ;
for(int j=0;j < strlen(string);j++){
switch(toupper(string[j])){
case 'A':;break;
case 'E':;break;
case 'I':;break;
case 'O':;break;
case 'U':;break;
default:const_count++;
}
}
return const_count;
}
int main(void){
char str[] = "this is a string 01 3.14"; /* actual string */
char buff[32] = { 0 }; /* a copy of actual string */
char delim[] = " "; /* used to specify the substrings */
char* token = NULL; /* used to store substring token from the actual string */
unsigned word_count =0;
strcpy(buff,str);
token = strtok(buff,delim);
/* (0) output provided data */
if(token != NULL){
printf("\n actual = '%s'\n",str);
printf(" delim = '%s'\n",delim);
}
/* (1) check if delim exists, show substring and increase word counter */
if(strlen(token) != strlen(str)){
printf(" substrings = '%s'%s",token,delim);
printf("(%d)",const_count(token));
word_count++;
}else
printf("\n (!) error, couldn't find '%s'\n",delim);
/* (2) proccess each substring if found any, show it and increase word counter */
while((token = strtok(NULL,delim)) != NULL){
printf("'%s'%s",token,delim);
printf("(%d)",const_count(token));
word_count++;
}
printf("\n");
printf("\n - total words in '%s' = %d words\n",str,word_count);
return 0;
}
| |