Hi, i have to write a program: user inputs a string of characters (letter, digits spaces etc any possible charachter) and then the program should calculate how many letter, digits, spaces, and other characters are there.
I did this question using just a char array and then for loop with a switch, like 100 cases: case 'A': case 'a' and all the way to the end of alphabet, and then count++; break; case 1, case 2 etc case 10, another count; break; so program is very long. Then i saw that i can use ctype.h, so i input a string of w.e., then something like that:

for (i=0; i<50; i++)
	{
		c=isalpha (string[i]);
		if (c!=0])
		{
			alpha++;
		}
		
	}

from my understanding, if the first character is a letter then c is gonna be something but 0, is its not a letter then its gonna be 0
but for some reason it doesnt work.
Any help would be appreciated.

Your understanding is faulty. You cannot pass a string into the isxxx() functions. You pass in a character and the return is non-zero for TRUE and 0 for FALSE.

ok, so then theres no way to use isalpha etc functions to calculate the number of letters etc in a string?

Not without using a loop or an stl algorithm.

Anyway C++ has its own version of this function is() declared in <local>

Not without using a loop or an stl algorithm.

Anyway C++ has its own version of this function is() declared in <local>

alright thanks, but how would i use a loop then? not like that?

for (i=0; i<50; i++)
	{
		c=isalpha (s[i]);
		if (c!=0])
		{
			alpha++;
		}
		
	}

alright thanks, but how would i use a loop then? not like that?

for (i=0; i<50; i++)
	{
		c=isalpha (s[i]);
		if (c!=0])
		{
			alpha++;
		}
		
	}

Yes. Although I'm make it more compact with:

i=0;
    while (s[i])    // Until the end of the string
    {
        if (isalpha (s[i]))  alpha++; 
        i++;        // Next character
    }

thanks for your help :)

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.