I'm trying to create a program in which a user inputs a string up to 100 characters, and the program prints the number of words, and the number of words that start with a capital letter. I have it displaying the correct number of words, but not words that start with a capital letter. Any help would be appreciated.
Code:#include <stdio.h> void sub(char mystring[100], int *word_count, int *cap_words, int *histogram) { int num_of_words, j, inside_a_word=0, word_cap=0; num_of_words = 0; // Calulates star of words with capital letters. if ( mystring[0]<=90 && mystring[0]>=65) { word_cap=1; } // Calculates number of words. for (j=0; j<100; j++) { if(mystring[j] == ' ' || mystring[j] == 0) { if (inside_a_word == 1) { num_of_words = num_of_words + 1; } inside_a_word = 0; } else { inside_a_word = 1; } // Calculates words that start with capital letters. if (mystring[j]== ' ') { if (mystring[j+1]<=90 && mystring[j+1]>=65); { word_cap++; } } if(mystring[j] == 0) { break; } } *word_count=num_of_words; *cap_words=word_cap; } int main(void) { char mystring[100]; int k, num_of_words, cap_words, histogram; printf ("Enter a string: \n"); // Obtains string from user. for (k=0; k<100; k++) { scanf("%c",&mystring[k]); if (mystring[k] == 10) { mystring[k] = 0; break; } } //printf("%s\n", mystring); sub (mystring, &num_of_words, &cap_words, &histogram); printf("The total number of words in the string = %d.\n", num_of_words); printf("The number of words starting with a capital letter = %d. \n", cap_words); }



LinkBack URL
About LinkBacks


