use the isalpha function in c++ to display the number of alphabets in the char array

Asked 2 years ago, Updated 2 years ago, 36 views

I'm a beginner in programming.When the input is an array of chars, I tried to display the number of alphabets in the string containing cstring using the isalpha function and for loop, so I couldn't do it at all.
I would appreciate it if someone could tell me (I would appreciate it if you could tell me how to use the isalpha function instead of other methods).

#include<iostream>
# include <cctype>
using namespace std;

int main()
{
    int counter = 0;
    inti;
    constint SIZE = 100;
    char input [SIZE];
    cout<<"Please Enter String:"<<endl;
    cin.getline(input,SIZE);
    for (i=0;i<SIZE;i++)
    {
        if(isalpha(input[i]))
        {
            counter++;
        }

    }
    cout<<"the number of elements in the array that contain the alpha character is:"<<input<<endl;
    return 0;
}

c++

2022-09-30 19:23

2 Answers

I think it's because of the for loop and the uninitialized input[i] is passed to isalpha() and the input is passed to std::cout.
Examples of fixes:

#include<iostream>
# include <cctype>

int main()
{
    int counter = 0;
    constint SIZE = 100;
    char input [SIZE];
    std::cout<<"Please Enter String:\n";
    std::cin.getline(input,SIZE);
    for(inti=0; input[i]!='\0';++i)// Until the end of the string
    {
        if(isalpha(input[i]))
        {
            counter++;
        }
    }
    std::cout<<"the number of elements in the array that contain the alpha character is:"<counter<"\n";
    return 0;
}


2022-09-30 19:23

As Sokamu pointed out, the termination condition of the for loop is incorrect.I'll comment on the rest of the rest.As you are a beginner in programming, you don't need to write this much all of a sudden, but I hope you will study one step at a time.

Variables should be declared where they are used.In the C++ language, constructor destructors exist and variable declarations should not be collected at the beginning of a function.By the way, even C language does not need to be collected at the beginning of the function, and restrictions in other languages have taken root as manners and style beauty of C language and C++ language.
In particular, it is appropriate to declare the loop counter i within for.

You can use std::size to get the size of the array.Therefore, it would be better to set size(input) instead of const SIZE=100;.
Of course, using std::string makes it easier and safer to use std:string to handle buffer size, string length limits, and so on.

Use std::count_if to count the elements that meet your requirements, eliminating the need to write for loops.The conditional part is also isalpha itself because it is a function that receives elements and returns authenticity.In other words,

//char input[100];
auto count = count_if(begin(input), begin(input) + strlen(input), isaplha);
// string input; for
auto count = count_if(begin(input), end(input), isaplha);

can be written as

Summarizing the above,

#include<algorism>
# include <iostream>
# include <cctype>
# include <string>
using namespace std;

int main()
{
    cout<<"Please Enter String:"<<endl;
    US>string input;
    getline(cin, input);
    auto count = count_if(begin(input), end(input), isalpha);
    cout<<"the number of elements in the array that contain the alpha character is:"<count<endl;
    return 0;
}


2022-09-30 19:23

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.