I have a question, pls help, thanks very much.


Try to write a C program which can analyze the input of the user in the following aspect.
1. vowels
2. consonants
3 digits
4 white spaces
5 others

You must use the logic of pointer to do this analysis in a function. there are some hints for you:
1. You may get the input of the user by using statement:
scanf(" %[^\n]",ling);
This statement can get the user input until he/she presses RETURN and put the characters in an array, in this example, it is called line[].

2. The prototype of the array can be like this:
void scan_line(char line[], int *pv, int *pc, int *pd, int *pw, int *po);

3. Inside the function you may use function toupper(char) to change each character you get from user to uppercase before you analyze them.

4. The output of the program will be like this:

====================================================
Please enter a line of text below:
Solider ! 88

no. of vowels: 3
No. of consonants: 4
no. of digits: 2
no. of whitespace characters: 2
No. of other characters: 1
=====================================================

Dani AI

Generated

asked for a pointer-based C routine to count vowels, consonants, digits, whitespace and "other" characters. The usual pattern is: read a line into a fixed buffer (use a safe input like fgets), walk the buffer using a pointer, normalize letters (with toupper) and update five integer counters passed by pointer. The sample input Solider ! 88 produces vowels 3, consonants 4, digits 2, whitespace 2 and other 1, which matches the numbers in the original post. 's "so.....whats the question 0_o?" is answered by the compact implementation below; 's link may show other approaches, but the pointer scan shown is simple and portable.

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void analyze_line(const char *s, int *pv, int *pc, int *pd, int *pw, int *po)
{
    const unsigned char *p = (const unsigned char *)s;
    *pv = *pc = *pd = *pw = *po = 0;

    while (*p) {
        unsigned char ch = *p;
        if (isspace(ch)) {
            (*pw)++;
        } else if (isdigit(ch)) {
            (*pd)++;
        } else {
            int up = toupper(ch);
            if (up == 'A' || up == 'E' || up == 'I' || up == 'O' || up == 'U')
                (*pv)++;
            else if (isalpha(ch))
                (*pc)++;
            else
                (*po)++;
        }
        p++;
    }
}

/* caller: read a line with fgets, strip trailing newline, then call analyze_line */

Common pitfalls and troubleshooting notes: always cast char to unsigned char when passing to ctype.h functions to avoid undefined behavior on platforms where char is signed. Initialize the five counters to zero before use (the function above does that). Prefer fgets over scanf with %s to prevent buffer overflows, and explicitly remove the trailing newline before counting if present. Be aware that isalpha, isdigit, isspace and toupper follow the current C locale; for strictly ASCII-only handling, test ranges 'A'..'Z' and 'a'..'z' instead.

If counts look wrong, check: (1) trailing newline not stripped, (2) signedness errors when calling ctype functions, (3) non-ASCII (UTF-8) input — the routine counts bytes, not Unicode graphemes. Compile with warnings enabled (-Wall -Wextra) and include the headers shown to catch common mistakes.

Recommended Answers

All 2 Replies

so.....whats the question 0_o?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.