void input_and_counting_vowels(unsigned short max) { unsigned short ctr = 0, x = 0, y = 0; unsigned short count_vowels = 0, count_consonants = 0; char ch = 0; unsigned char vowels[5+1]; unsigned char consonants[max+1]; for(ctr = 0; ctr < max; ctr++) { printf("\tВведите %2hu символ из %2hu: ", ctr+1, max); if((ch = getchar()) == EOF) { fputs("\nПроизошел сбой при вводе символа!\n", stderr); exit(EXIT_FAILURE); } if(ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { vowels[x] = ch; x++; count_vowels++; } else if(ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { vowels[x] = ch; x++; count_vowels++; } else { consonants[y] = ch; y++; } clear_stdin(); } vowels[x] = '\0'; consonants[y] = '\0'; count_consonants = max - count_vowels; printf("\nВы ввели %hu символов.\n", max); printf("\tГласных: %s (%hu).\n", vowels, count_vowels); printf("\tСогласных: %s (%hu).\n", consonants, count_consonants); } max - the number of characters entered by the user. My function is clear_stdin () instead of fflush (stdin), since fflush () for some reason does not work.
The vowels [], consonants [] arrays store all the vowels and consonants entered by the user, respectively. +1 for the end of line character to output an array as a string.
Questions:
- Is it possible to allocate memory for arrays in some way (vowels [], consonants [])?
- Is there another way to count vowels and consonants?
- Why does Microsoft Visual C ++ 2010 Express swear by max (requires constant expression)? Whereas gcc in cygwin compiles without problems.
- Is it possible to improve this function in general?
I am still studying C (this is my first programming language), so tips, recommendations and constructive criticism are welcome.
char chcompared to the EOF in theif. In the cp1251 encoding (Russians in Windows) this is the letter 'I'. What happens if more than 5 vowels are entered? ( The vowels [] array will overflow and rub something in memory. ) - Probably, if you want to store and then separately type the entered vowels and so on, you need either 2 arrays ofmax + 1size, or one ofmax + 2which is filled from two ends (towards each other), and then one of the ends is reversed. Unnecessary +2 - this is for zeroes that fill the character strings (if you type laziness in the loop yourself). - avpn_vowels-n_vowelsandtotal. - In M $, obviously, in some of themaxalready somehow defined (most likely it is a macro or a function). - Further it is supposed that speech about single-byte characters. If you want to store information exactly which characters were entered at least once - 256 bits are enough (line 16 bytes). If you need to know how many times each character has been entered (order is not important), then an array of 256 counters is enough. Naturally, before use, both must be initialized. - avp-1typeint, and if you assign it tochar'y, it is converted to 255, which corresponds to Russian'я'in one of the encodings. Thegetchar()function returnsintfor a reason. - VladDgetchar()returns anint, and you have a variable of typechar. Never usecharwhereintassumed. In this example,int(-1)is notchar(-1), it is 255, it is 0xFF. - user6550 pm