Reputation: 21
#include <stdio.h>
#include <string.h>
#define SIZE 30
int countWord(int a[]);
int countSpace(int a[]);
int countVowel(int a[]);
int printResult(int a[]);
int main()//starting main
{
char string1[SIZE];
printf("%s","Enter a string less than 29 characters");
scanf("%29[^\n]",string1); //anything except \n.
printResult(string1);
}
int countWord(int a[]){
int count=1;
for(size_t i=0;i<SIZE&&a[i]!='\0';++i){
if(a[i]==' '){
count++;
}
}
return count;
}
int countSpace(int a[]){
int count1=0;
for(size_t i=0;i<SIZE&&a[i]!='\0';++i){
if(a[i]==' '){
count1++;
}
}
return count1;
}
int countVowel(int a[]){
int count2=0;
for(size_t i=0;i<SIZE&&a[i]!='\0';++i){
if(strchr("aeiouAEIOU", a[i])){
count2++;
}
}
return count2;
}
int countNum(int a[]){
int count3=countWord(a)-countVowel(a)-countSpace(a);
return count3;
}
int printResult(int a[]){
printf("Your sentence include\n");
printf("Number of words:%d\n",countWord(a));
printf("Number of spaces:%d\n",countSpace(a));
printf("Number of vowels:%d\n",countVowel(a));
printf("Number of consonants and special characters:%d\n",countNum(a));
}
this program is about asking user to input a string and calculate how many words,space,vowels... but I get very wrong result after the enter a string. I thought the logic is fine and was struggling to find any issues here
Upvotes: 0
Views: 624
Reputation: 153368
struggling to find any issues here
Save time, enable all warnings. That is the key mistake.
Review types.
int printResult(int a[]);
char string1[SIZE];
printResult(string1);
Word count errors with " "
, " abc def "
, " abcd"
.
Alternative: set a flag for the start of a word.
int countWord(int a[]){
int countWord(const int_but_I_suspect_you_want *a) {
int count=0;
int start_of_word_possible = 1;
while (*a) {
if (isspace(*a)) {
start_of_word_possible = 1;
} else {
if (start_of_word) count++;
start_of_word_possible = 0;
}
a++;
}
return count;
}
Upvotes: 1
Reputation: 23218
Without knowing the original string you entered, it is not possible to know what unexpected result you saw, but here are general suggestions:
int a[]
for const char *a
in all of your function prototypes and functions. anything except \n.
after the scanf
call in main
. int printResult(const char *a
, make it void printResult(const char *a
. main
is int main(void){...return 0;}
.With only these changes, and upon entering This is a string
, I get this result:
Number of words:4
Number of spaces:3
Number of vowels:4
Number of consonants and special characters:-3
Because 4 - 3 - 4 == -3, it appears all the values were calculated correctly.
Code updated per my comments above:
int main(void)//starting main
{
char string1[SIZE];
printf("%s","Enter a string less than 29 characters");
scanf("%29[^\n]",string1);//anything except \n.
printResult(string1);
return 0;
}
int countWord(const char *a){
int count=1;
for(size_t i=0;i<SIZE&&a[i]!='\0';++i){
if(a[i]==' '){
count++;
}
}
return count;
}
int countSpace(const char *a){
int count1=0;
for(size_t i=0;i<SIZE&&a[i]!='\0';++i){
if(a[i]==' '){
count1++;
}
}
return count1;
}
int countVowel(const char *a){
int count2=0;
for(size_t i=0;i<SIZE&&a[i]!='\0';++i){
if(strchr("aeiouAEIOU", a[i])){
count2++;
}
}
return count2;
}
int countNum(const char *a){
int count3=countWord(a)-countVowel(a)-countSpace(a);
return count3;
}
void printResult(const char *a){
printf("Your sentence include\n");
printf("Number of words:%d\n",countWord(a));
printf("Number of spaces:%d\n",countSpace(a));
printf("Number of vowels:%d\n",countVowel(a));
printf("Number of consonants and special characters:%d\n",countNum(a));
}
Upvotes: 0