私は明らかにフォーラムに不慣れで、助けが必要です。文中の母音、単語、回文の数を表示するプログラムを書いています。私はロープの終わりにいて、完全に迷っています。
私は C を使用しており、入力内の単語をループして、文に含まれる回文の数を判断しようとしています。これはカウンター内に保存され、後でメイン メソッド内に出力されますが、作成には支援が必要です上記のループは、カウンターに値を適切に格納するためのものです。
これが私がこれまでに持っているものです(私は特に見ていint is_palindrome(char my_sen[])
ます)
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define SENTENCE 256
int main(void){
char my_sen[SENTENCE],*s; //String that containts at most 256 as well as a pointer
int words = 1, count = 0, pal_count= 0; //Integer variables being defined
int i,vowel = 0, length; //More definitions
printf("Enter a sentence: ");//Input sentence
gets(my_sen);//Receives and processes input
length = strlen(my_sen); //Stores the length of the input within length
for(i=0;my_sen[i] != '\0'; i++){
if(my_sen[i]=='a' || my_sen[i]=='e' || my_sen[i]=='i' || my_sen[i]=='o' || my_sen[i]=='u' || //Loop that states if the input contains any of the following
my_sen[i]=='A' || my_sen[i]=='E' || my_sen[i]=='I' || my_sen[i]=='O' || my_sen[i]=='U') //characters(in this case, vowels), then it shall be
{ //stored to be later printed
vowel++;
}
if(my_sen[i]==' ' || my_sen[i]=='!' || my_sen[i]=='.' || my_sen[i]==',' || my_sen[i]==';' || //Similar to the vowel loop, but this time
my_sen[i]=='?') //if the following characters are scanned within the input
{ //then the length of the characters within the input is
length--; //subtracted
}
}
for(s = my_sen; *s != '\0'; s++){ //Loop that stores the number of words typed after
if(*s == ' '){ //each following space
count++;
}
}
printf("The sentence entered is %u characters long.\n", length); //Simply prints the number of characters within the input
printf("Number of words in the sentence: %d\n", count + 1); // Adding 1 to the count to keep track of the last word
printf("Average length of a word in the input: %d\n", length/count);//Prints the average length of words in the input
printf("Total Number of Vowels: %d\n", vowel);//Prints the number of vowels in the input
printf("Average number of vowels: %d\n", vowel/count);//Prints the average number of vowels within the input
printf("Number of words that contain at least 3 vowels: %d\n",vowel_count(my_sen));//Prints number of words that contain at least 3 vowels
printf("Number of words that are palindomes: %d\n", is_palindrome(my_sen));
return 0;
}
int vowel_count(char my_sen[])
{
int wcount = 0;
int vcount = 0;
int i = 0;
int ch;
while ((ch = my_sen[i++]) != '\0')
{
if (isspace(ch) || !isalpha(ch))
{
wcount += vcount >= 3;
vcount = 0;
continue;
if (strchr("aeiouAEIOU", ch) != NULL)
{
++vcount;
}
}
wcount += vcount >= 3; // add 1 to wcount if vcount >= 3
return wcount;
}
int is_palindrome(char my_sen[]){
int begin, middle, end, length = 0, result = 0, pal = 0; //variables representing the string length, beginning, middle, and end of string
while ( my_sen[length] != '\0' ) //loop to define and initialize variables
length++;
end = length - 1; //end is the end of the length
middle = length/2;//middle is half the length
for( begin = 0 ; begin < middle ; begin++ )
{
if ( my_sen[begin] != my_sen[end] ) //if the beginning isn't equal to the end, then it's not a palindrome
{
result += pal;
pal = 0;
continue;
}
end--;
}
if( begin == middle ) //if the beginning is the same as the middle, its a palindrome
pal++;
result += pal;
return result;
}