7 Replies - 1507 Views - Last Post: 24 March 2008 - 11:16 AM Rate Topic: -----

#1 KMH  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 23-March 08

cctype functions

Post icon  Posted 23 March 2008 - 04:18 PM

Hello,

I wrote a C++ code below using cctype functions, it did not work, what did I do wrong?? Please help.
 #include <iostream>
#include <string>
#include <cstring>
#include <cctype>  //Function type for character functions
int main()

{
	using namespace std;
	int charr;
	int i = 0
	char charr[] = "Hello all, This is the 1st prolem in lesson 6.\n";
	
	while (charr[i])
	
	

	// Initialize the cctype Character Functions
	int chars = 0;
	int uppercase = 0;
	int lowercase = 0;
	int digits = 0;
	int whitespace = 0;
	int punct = 0;
	int alphanumeric = 0;
	int others = 0;

	getline(cin, charr); // Get the first character string
	while(charr != '*')
	
	{
		if(isalpha(charr)) // test to see if it is an alphabetic character?
			chars++;
		else if(isupper(charr)) //Is it uppercase character?
			uppercase++;
		else if(islower(charr)) //Is it lowercase character?
			lowercase++;
		else if(isdigit(charr)) //Is it a decismal digit(0-9)?
			digits++;
		else if(isspace(charr)) //Is it a whitespace character?
			whitespace++;
		else if(ispunct(charr)) //Is it punctuation?
			punct++;
		else if(isalnum(charr)) //Is it alphanumeric(a letter or a digit)
			alphanumeric++;
		else
			others++;
		getline(cin, charr); //get the next charactter
	}
	cout << chars << " character" << endl;
	cout << uppercase << " uppercase character" << endl;
	cout << lowercase << " lowercase character" << endl;
	cout << digits << " decimal digit" << endl;
	cout << whitespace << " space" << endl;
	cout << punct << " punctuation" << endl;
	cout << alphanumeric << " alphanumeric(that is, a letter or a digt)" << endl;
	cout << others << " others.\n" << endl;
 
 cin.get();
 cin.get();
 return 0;
}


Is This A Good Question/Topic? 0
  • +

Replies To: cctype functions

#2 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: cctype functions

Posted 23 March 2008 - 04:34 PM

Hello and welcome to </dream.in.code> Have a great time here, increasing your programming skills.

There were a couple of mistakes you made in the code that were corrected and now you can learn from the comments inside the code:


#include <iostream>
#include <string>
#include <cstring>
#include <cctype> //Function type for character functions
using namespace std;

int main() {
	
	
	int i = 0;	//missed a semi-colon
	char charr[] = "Hello all, This is the 1st prolem in lesson 6.\n";





	
	int chars = 0;
	int uppercase = 0;
	int lowercase = 0;
	int digits = 0;
	int whitespace = 0;
	int punct = 0;
	int alphanumeric = 0;
	int others = 0;


	while(charr[i] != '\0') { 
		//need to pass each element of the char array to the functions
		if(isalpha(charr[i])) // test to see if it is an alphabetic character?
			chars++;

		else if(isupper(charr[i])) //Is it uppercase character?
			uppercase++;

		else if(islower(charr[i])) //Is it lowercase character?
			lowercase++;

		else if(isdigit(charr[i])) //Is it a decismal digit(0-9)?
			digits++;

		else if(isspace(charr[i])) //Is it a whitespace character?
			whitespace++;

		else if(ispunct(charr[i])) //Is it punctuation?
			punct++;
		else
			others++;

		if(isalnum(charr[i])) //Is it alphanumeric(a letter or a digit)
			alphanumeric++;



		i++; //incrementing i to get the next element

	}


	cout << chars << " character" << endl;
	cout << uppercase << " uppercase character" << endl;
	cout << lowercase << " lowercase character" << endl;
	cout << digits << " decimal digit" << endl;
	cout << whitespace << " space" << endl;
	cout << punct << " punctuation" << endl;
	cout << alphanumeric << " alphanumeric(that is, a letter or a digt)" << endl;
	cout << others << " others.\n" << endl;

	cin.get();
	cin.get();
	return 0;

}




Also, please edit your post, and place your code in the code tags :code: , for better readability.

Hope that helps.

This post has been edited by letthecolorsrumble: 23 March 2008 - 04:36 PM

Was This Post Helpful? 0
  • +
  • -

#3 KMH  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 23-March 08

Re: cctype functions

Posted 23 March 2008 - 04:52 PM

That was it, thank you very much for your help, I know I have a lot to learn and I have not done programing since I was in college. How would convert all the string to uppercase and lowercase using tolower and toupper??
Was This Post Helpful? 0
  • +
  • -

#4 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: cctype functions

Posted 23 March 2008 - 05:00 PM

You will find the help you are looking for converting cases of strings:

http://www.dreaminco...e/snippet39.htm

tolower(char) and toupper(char) will be the functions you need to convert the cases.

example:

char ch1 = 's';
char ch2 = toupper(ch1);




As you can see you can only convert one character at a time, so you will need a char-array to convert the cases using a loop.

More help can be found in the above link.

Hope that helps. :)
Was This Post Helpful? 0
  • +
  • -

#5 KMH  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 23-March 08

Re: cctype functions

Posted 24 March 2008 - 09:29 AM

Yes that work well, thank for all your help, for some reason the code isupper and islower did not display the number of upper and lower cases characters, is it have something to do with the system I used (MS visual studio 2008)???

View Postletthecolorsrumble, on 23 Mar, 2008 - 05:00 PM, said:

You will find the help you are looking for converting cases of strings:

http://www.dreaminco...e/snippet39.htm

tolower(char) and toupper(char) will be the functions you need to convert the cases.

example:

char ch1 = 's';
char ch2 = toupper(ch1);




As you can see you can only convert one character at a time, so you will need a char-array to convert the cases using a loop.

More help can be found in the above link.

Hope that helps. :)

Was This Post Helpful? 0
  • +
  • -

#6 letthecolorsrumble  Icon User is offline

  • Student of The Sun
  • member icon

Reputation: 27
  • View blog
  • Posts: 555
  • Joined: 07-November 07

Re: cctype functions

Posted 24 March 2008 - 10:57 AM

Modifying your code a bit you can count the uppercase and the lowercase letter in your text.

Since you place the counting of the cases, inside the if-else if ladder, it at time it makes the code unreachable and thus never increase the number of lower or upper case characters in your code.


		//need to pass each element of the char array to the functions
		if(isalpha(charr[i])) // test to see if it is an alphabetic character?
			chars++;

		else if(isdigit(charr[i])) //Is it a decismal digit(0-9)?
			digits++;

		else if(isspace(charr[i])) //Is it a whitespace character?
			whitespace++;

		else if(ispunct(charr[i])) //Is it punctuation?
			punct++;
		else
			others++;

		if(isalnum(charr[i])) //Is it alphanumeric(a letter or a digit)
			alphanumeric++;

		if(isupper(charr[i])) //Is it uppercase character?
			uppercase++;

		if(islower(charr[i])) //Is it lowercase character?
			lowercase++;





Hope that helps.
Was This Post Helpful? 0
  • +
  • -

#7 KMH  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 23-March 08

Re: cctype functions

Posted 24 March 2008 - 11:09 AM

Hi;

yes that work but it seem like some of these (cctype / isxxx) functions work best if I use only IF instead of ELSE IF statement, but do I know when to use ELSE IF and just IF???

Best Regards,

KMH


View Postletthecolorsrumble, on 24 Mar, 2008 - 10:57 AM, said:

Modifying your code a bit you can count the uppercase and the lowercase letter in your text.

Since you place the counting of the cases, inside the if-else if ladder, it at time it makes the code unreachable and thus never increase the number of lower or upper case characters in your code.


		//need to pass each element of the char array to the functions
		if(isalpha(charr[i])) // test to see if it is an alphabetic character?
			chars++;

		else if(isdigit(charr[i])) //Is it a decismal digit(0-9)?
			digits++;

		else if(isspace(charr[i])) //Is it a whitespace character?
			whitespace++;

		else if(ispunct(charr[i])) //Is it punctuation?
			punct++;
		else
			others++;

		if(isalnum(charr[i])) //Is it alphanumeric(a letter or a digit)
			alphanumeric++;

		if(isupper(charr[i])) //Is it uppercase character?
			uppercase++;

		if(islower(charr[i])) //Is it lowercase character?
			lowercase++;





Hope that helps.

Was This Post Helpful? 0
  • +
  • -

#8 KMH  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 80
  • Joined: 23-March 08

Re: cctype functions

Posted 24 March 2008 - 11:16 AM

I need to convert "Hello all, This is the 1st problem in lesson 6." to an array of strings each of which contains on word. Using cctype function and if/else statement, how would I start???


#include <iostream>
#include <string>
#include <cstring>
#include <cctype>  //Function type for character functions
int main()

{
	using namespace std;
	int i = 0;

	mystring charr= "Hello all, This is the 1st prolem in lesson 6.\n";
	//size = strlen(mystring);
	//strArray = ArrDimension(80)
	//string fruit = "Apple,Banana,Orange,Strawberry";
	mystring[] split = charr.Split(',');
	char arr[] = charr. toCharArray();
	for (int i=0; i<array.length; i++)			

	for(int i=0;i<50;i++)
	mychar[i]=mystring[i];
	cout<<mychar;


	
 cin.get();
 return 0;
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1