Answer the question
In order to leave comments, you need to log in
How to extract characters from a string?
Hello! Please help, I was given the following task: "A program that reads a string of up to 20 characters from the user (it is necessary to find out if the string does not exceed the specified length, if so, then it must be re-entered), counts and lists the number of vowels, consonants, punctuation marks, numbers and other characters (separated by commas) Example: word Auto, vowels: 3 - A, u, o, etc." .
Code for searching and counting vowels, consonants, etc. has already. I don’t know how to now display all the counted characters from the string, and how to set the condition that if the string is more than 20 characters, then you need to enter it again? Help me please.
#include <stdio.h>
#include <string.h>
#define N 21
int main(){
char str[N];
const char vovels[]="eyuioaEYUIOA";
const char consonants[]="qwrtpsdfghjklzxcvbnmQWRTPFGHJKLZXCVBNM";
char digits[]="0123456789";
const char punctuation[]=".,:;!?\"()-";
const char symbols[]="@#$%^&*{}[]<>_+=\'¹%|\\/`~";
const char spaces[]=" ";
int v, c, d, p, s, sp;
v = c = d = p = s = sp = 0;
printf("Enter a string no more than 20 symbols :\n\n");
gets(str);
for (int i=0; str[i] != '\0'; i++)
if (strchr(vovels,str[i])) v++;
else if (strchr(consonants,str[i])) c++;
else if (strchr(digits,str[i])) d++;
else if (strchr(punctuation,str[i])) p++;
else if (strchr(symbols,str[i])) s++;
else if (strchr(spaces,str[i])) sp++;
printf("\nVowels: %d\n", v);
printf("\nConsonants: %d\n", c);
printf("\nDigits: %d \n", d);
printf("\nPunctuation: %d\n", p);
printf("\nSymbols: %d\n", s);
printf("\nSpaces: %d\n", sp);
return 0;
}
Answer the question
In order to leave comments, you need to log in
man strlen (and it's even inconvenient to write like that - strlen() is one of the first string functions :) )
man calloc
To enumerate, you need to create separate arrays and copy a character there if the condition is met. And then just output this array.
In addition, static arrays are evil, memory must be taken dynamically. I don't include the code on purpose because you are learning and your learning is about learning what string functions are and not about copy-pasting. As a basis, this program is suitable. But in it you need to:
- replace static memory with dynamic
- after reading the line, check its length, if it is > 20 - ask again, which naturally leads us to the fact that the request will be made in an infinite loop
- in the disassembly if, not only twist the necessary counters, but also copy the character to the corresponding array.
- after parsing, output all selected arrays.
First of all, I strongly recommend that you first familiarize yourself with the basics of the language, as well as the I/O functions you use. The C language is very simple: if you want to shoot yourself in the foot, you shoot yourself.
A quick look at your code shows that you don't care about the input. For example, this construction, although it allows you to count a string, does not give any guarantees that it will not exceed the size of 21 characters.
#define N 21
...
char str[N];
printf("Enter a string no more than 20 symbols :\n\n");
gets(str);
for (int i=0; str[i] != '\0'; i++)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question