Write a program that counts words, characters, uppercase letters, lowercase letters, digits
and special characters in a phrase

Please have a go at doing this. Then we are happy to help.

mat bro I can't..I don't even know how to start this after first initial code.please help me!

Following code count digits in a string.

#include <iostream>
#include <string>

using namespace std;

int main()
{

    string str = "hel22lo35Wo4rld";    //string with digits
    int digitCount = 0;

    //string is char of array so we will check each char whether it is a digit or not
    //by using loop

    for(int i=0; i<str.length(); i++)  
    {
        if(isdigit(str[i]) && str[i] != '\0')
        {
            digitCount++;
        }
        cout<<str[i]<<" ";
    }

    cout<<"\ndigits in string is : " <<digitCount <<endl;

    return 0;
}

Hope this helps in case of digits count !

for(int i=0; i<str.length(); i++)
{
if(isdigit(str[i]) && str[i] != '\0')

could you please explain these two lines for me as I'm a beginner please!

Oh, I see. Oh dear me.

OK, then, I will start with the if() statement, as it is the easier of the two to understand. Basically, what an if() statement does is take a true or false value, and based on that value, select from one of two paths to take. The general form of it is

if (<conditional_expression>) 
    <true_case>
else
    <false_case>

In the case where you only need to test for a true case, you can omit the else and the false case.

This particular if() statement has two parts, connected by an AND operator (the && part). This means that the condition is true if and only if both of the clauses on the two sides of the operation are true.

The first part tests whether a given character is a digit, that is, whether the character value is a '0', or a '1', or a '2', and so on through '9'. The isdigit() function (technically a macro in most implementations, but that's not important here) is a standard library routine that does this test. The second part of the condition checks (somewhat redundantly, as it happens) whether it the given character is not equal to the NUL character, that is, a character which is encoded as the number zero (which is not the same as the character '0', BTW; see the ASCII table for a quick review of the character encoding values). If both of these are true, then it performs the next statement following the if(), which in this case happens to be a compound statement, that is, a statement consisting of one or more statements between an open brace ({) and a close brace (}). The full statement that it executes is:

{
    digitCount++;
}

This has the effect of incrementing the value of digitCount by one. The increment operator (++) is a shorthand way of writing

    digitCount = digitCount + 1;

It is used in the next part I'm going to explain now, the for() loop.

The for() loop is what is known as a definite iteration, which means that it repeats a fixed number of times which is known ahead of time. This is in contrast to the while() loop which is an indefinite iteration, or one which repeats a number of times which depends on the results of the loop body itself. The general form of the for() loop is

for (<initializer> ; <condition> ; <increment> )
    <loop_body>

Like with the if() statement, the loop body can be a compound statement, and usually is. In fact, it is a good practice to always use braces around a loop body, even if it only one line, to make it stand out clearly.

This particular loop has a loop index, the variable i, which is initialized to the value 0. The initializer, as the name implies, is only run once, at the start of the loop. The condition that the loop repeats on is whether i is less than the length of the string str, which is the string to be tested. If i is less than str.length(), then the body of the loop is executed once. When the body of the loop has been executed, the increment part of the loop, which in this case is i++, is run. This means that the value of i steps through the length of the string until it reaches the end of it. The body of the loop is:

    {
        if(isdigit(str[i]) && str[i] != '\0')
        {
            digitCount++;
        }
        cout<<str[i]<<" ";
    }

Thus, the overall meaning of the loop is, "for every character in 'str', check to see if it is a digit, and if it is, add one to the count of digits; then print out the character followed by a space". Is this clearer now?

for(int i=0; i<str.length(); i++)

This is a for loop, which repeat instructions or set of instructions according to the value of loop control variable (int i = 0) until condition is remain true (i<str.length()). Third part(i++) increments value of i.

Moreover the purpose of this loop is to check the string, index by index for digits. Because string is the array of characters. So its first index starts with 0 that's why loop control variable int i starts with 0.

It will continue looping until the condition is true that is i value is less than the length of string characters array.

length() function returns length or number of characters in array.

For example str = "Hello";

str.length() return 5 as its length.

if(isdigit(str[i]) && str[i] != '\0')

Second line is if condition, in which i have used isdigit function to check each index(or each char) in string if it is a digit. 2nd part (str[i] != '\0') is not necessary.

If this helps please vote up for me. Thanks !

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.