С клавиатуры вводится текстовая строка. Разработать программу, которая реализует указанные действия.
а) выводит количество слов, которые содержат одинаковое количество гласных и согласных букв;
б) выводит на экран самое длинное слово.
C/C++
Нужен код на С. Подробней в описании
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char vowels[] = "aeiouy"; /* гласные - 6 */
char consonants[] = "bcdfghjklmnpqrstvwxz"; /* согласные буквы - 20 */
const int SIZE = 1024;
/* определим, гласная ли буква */
int is_vowel(const char c)
{
for(int x = 0; x < 6; x++)
if (tolower(c) == vowels[x]) return 1;
return 0;
}
/* определим, согласная ли буква */
int is_consonant(const char c)
{
for(int x = 0; x < 20; x++)
if (tolower(c) == consonants[x]) return 1;
return 0;
}
/* считаем количество гласных в слове */
int count_vowels(const char * word, const int len)
{
int count = 0;
for(int x = 0; x < len; x++)
if( is_vowel(word[x]) ) count++;
return count;
}
/* считаем количество согласных в слове */
int count_consonants(const char * word, const int len)
{
int count = 0;
for(int x = 0; x < len; x++)
if( is_consonant(word[x]) ) count++;
return count;
}
/* для хранения слов и информации о них */
struct words
{
char * word; /* слово */
int len; /* длина слова */
int volwes; /* количество гласных в слове */
int consonants; /* количество согласных в слове */
};
int main()
{
char str[SIZE]; /* строка */
char c;
int i = 0;
while( ( (c = getchar()) != '\n') && i < SIZE - 1 )
{
str[i] = c;
i++;
}
str[i] = '\0';
struct words w[SIZE]; /* хранилище слов */
int count_of_words = 0; /* счётчик слов */
int len_str = strlen(str); /* длина введённой строки */
/* разбиваем строку на слова */
for(int x = 0; x < len_str;)
{
w[count_of_words].word = (char *) malloc(SIZE); /* выделяем память под слово */
w[count_of_words].len = 0;
w[count_of_words].volwes = 0;
w[count_of_words].consonants = 0;
while( ( is_vowel(str[x]) || is_consonant(str[x]) ) && x < len_str )
{
if( is_vowel(str[x]) ) w[count_of_words].volwes++;
else if ( is_consonant(str[x]) ) w[count_of_words].consonants++;
w[count_of_words].word[w[count_of_words].len] = str[x];
w[count_of_words].len++;
x++;
}
w[count_of_words].word[w[count_of_words].len] = '\0';
count_of_words++;
/* пропустить знаки препинания, пробелы */
while( !( is_vowel(str[x]) || is_consonant(str[x]) ) && x < len_str ) x++;
}
/* а) */
int odinak = 0; /* сколько слов с одинаковым количеством гласных и согласных */
for(int x = 0; x < count_of_words; x++)
if(w[x].volwes == w[x].consonants) odinak++;
printf("Количество слов с одинаковым количеством гласных и согласных равно %d.\r\n", odinak);
/* б) */
int i_max_len = 0; /* индекс слова с максимальной длиной */
for(int x = 1; x < count_of_words; x++)
if( w[x].len > w[i_max_len].len ) i_max_len = x;
printf("Самое длинное слово -- %s.\r\n", w[i_max_len].word);
/* очищаем память */
for(int x = 0; x < count_of_words; x++)
{
if(!w[x].word)
{
w[x].word[0] = ' ';
w[x].word[1] = '\0';
}
free(w[x].word);
w[x].word[0] = '\0';
w[x].len = 0;
w[x].volwes = 0;
w[x].consonants = 0;
}
return 0;
}
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
char vowels[] = "aeiouy"; /* гласные - 6 */
char consonants[] = "bcdfghjklmnpqrstvwxz"; /* согласные буквы - 20 */
const int SIZE = 1024;
/* определим, гласная ли буква */
int is_vowel(const char c)
{
for(int x = 0; x < 6; x++)
if (tolower(c) == vowels[x]) return 1;
return 0;
}
/* определим, согласная ли буква */
int is_consonant(const char c)
{
for(int x = 0; x < 20; x++)
if (tolower(c) == consonants[x]) return 1;
return 0;
}
/* считаем количество гласных в слове */
int count_vowels(const char * word, const int len)
{
int count = 0;
for(int x = 0; x < len; x++)
if( is_vowel(word[x]) ) count++;
return count;
}
/* считаем количество согласных в слове */
int count_consonants(const char * word, const int len)
{
int count = 0;
for(int x = 0; x < len; x++)
if( is_consonant(word[x]) ) count++;
return count;
}
/* для хранения слов и информации о них */
struct words
{
char * word; /* слово */
int len; /* длина слова */
int volwes; /* количество гласных в слове */
int consonants; /* количество согласных в слове */
};
int main()
{
char str[SIZE]; /* строка */
char c;
int i = 0;
while( ( (c = getchar()) != '\n') && i < SIZE - 1 )
{
str[i] = c;
i++;
}
str[i] = '\0';
struct words w[SIZE]; /* хранилище слов */
int count_of_words = 0; /* счётчик слов */
int len_str = strlen(str); /* длина введённой строки */
/* разбиваем строку на слова */
for(int x = 0; x < len_str;)
{
w[count_of_words].word = (char *) malloc(SIZE); /* выделяем память под слово */
w[count_of_words].len = 0;
w[count_of_words].volwes = 0;
w[count_of_words].consonants = 0;
while( ( is_vowel(str[x]) || is_consonant(str[x]) ) && x < len_str )
{
if( is_vowel(str[x]) ) w[count_of_words].volwes++;
else if ( is_consonant(str[x]) ) w[count_of_words].consonants++;
w[count_of_words].word[w[count_of_words].len] = str[x];
w[count_of_words].len++;
x++;
}
w[count_of_words].word[w[count_of_words].len] = '\0';
count_of_words++;
/* пропустить знаки препинания, пробелы */
while( !( is_vowel(str[x]) || is_consonant(str[x]) ) && x < len_str ) x++;
}
/* а) */
int odinak = 0; /* сколько слов с одинаковым количеством гласных и согласных */
for(int x = 0; x < count_of_words; x++)
if(w[x].volwes == w[x].consonants) odinak++;
printf("Количество слов с одинаковым количеством гласных и согласных равно %d.\r\n", odinak);
/* б) */
int i_max_len = 0; /* индекс слова с максимальной длиной */
for(int x = 1; x < count_of_words; x++)
if( w[x].len > w[i_max_len].len ) i_max_len = x;
printf("Самое длинное слово -- %s.\r\n", w[i_max_len].word);
/* очищаем память */
for(int x = 0; x < count_of_words; x++)
{
if(!w[x].word)
{
w[x].word[0] = ' ';
w[x].word[1] = '\0';
}
free(w[x].word);
w[x].word[0] = '\0';
w[x].len = 0;
w[x].volwes = 0;
w[x].consonants = 0;
}
return 0;
}
Максим Крюков
Программа работает только со словами, написанными на латинице.
***sanjar*** ***anarbaev***
Спасибо.
Похожие вопросы
- Очень нужна помощь. нужен код на C. Задачка будет в описании.
- Очень нужен код на c++ (тема: ГРАФЫ)
- Программа на с++ (нужен код) пожалуйста помогите
- Задание с использование библиотечных функций С++. Нужен код. Помогите, пожалуйста.
- Нужен код на Си помогите пж
- Функция на рекурсию. Нужен код на С
- Нужен код на Си помогите пожалуйста
- Нужен код на Си помогите пж
- Нужен код на С :)
- Нужен код на С++