A
A
Artemy Gavrilov2020-01-24 22:58:18
C++ / C#
Artemy Gavrilov, 2020-01-24 22:58:18

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

2 answer(s)
C
CityCat4, 2020-01-25
@CityCat4

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.

W
Wundarshular, 2020-02-11
@Wundarshular

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);

And here you are naively hoping that the line definitely has an end. The C language is very simple: if you want to count a string, you count it. But how you get it is your problem. As for your task, I believe that the easiest way in your case will be a loop in a loop: with the main loop you go through the received string, with side loops look for matches in your directories (vovels arrays, etc.) and when you find it, either display it immediately, or copy the character somewhere for output later. You will greatly simplify the code in this part if you look at the ASCII table. The task of limiting the length is solved, for example, by reading the language reference - all-ht.ru/inf/prog/c/func/fgets.html
for (int i=0; str[i] != '\0'; i++)
- or simply unceremonious chopping off of the entered line. For forcing repeated input, I think while or do while is fine for you - whichever you prefer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question