13 Replies - 5062 Views - Last Post: 07 June 2007 - 09:43 AM Rate Topic: -----

#1 summer291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 07

Counting characters in a String using a function

Post icon  Posted 03 June 2007 - 06:06 AM

I'm trying to count characters in a string using my own function which is Char_Count, when I try to compile I get this error message.
Char_Count' : local function definitions are illegal.


#include <iostream>

using namespace std;

int Char_Count (char*);

int main()
{
  char ch;
  char line[81];
  char* ch_ptr;
  

  cout << "Enter a line of words:" << endl << endl;
  cin.getline(line, 81);

  cout << "\nEnter a character:" << endl << endl;
  ch = cin.get();



  // Skip any blanks at the beginning of the line
  for (ch_ptr = line; *ch_ptr == ' '; ++ch_ptr);
  return 0;
 int Char_Count(char* ch_ptr)
{

	int  letter_count = 0
  // Process rest of line
  do	  
  {
	  if (*ch_ptr == ch)
	  {
		  ++ch_ptr;
		  ++letter_count;			   // Count the letter 
	  }
	  else
		  ++ch_ptr;

	// Skip the word
	//  for (; (*ch_ptr != '\0'); ++ch_ptr);	 ;

	// Skip blanks after the word
	 // for (; *ch_ptr == ' '; ++ch_ptr);
  }
while (*ch_ptr != '\0');  
;
cout << "\nThe line you entered has " << letter_count;
cout << " occurrences of the character " << ch << "." <<endl;

  return letter_count;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Counting characters in a String using a function

#2 v0id  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 4
  • Joined: 22-November 06

Re: Counting characters in a String using a function

Posted 03 June 2007 - 07:26 AM

The code looks very weird, try clean up, and you'll find the bug.
Try take a look at this. This is how I would make a function to count the ammount of characters in a string.
int Char_Count(char *szStr)
{
	int iCount = 0;
	while(*szStr++ != '\0') iCount++;
	return iCount;
}


This post has been edited by v0id: 03 June 2007 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

#3 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Counting characters in a String using a function

Posted 03 June 2007 - 01:50 PM

The problem occurs (well, the specific error you talked about) because you have defined the function within the main function. Move your function definition outside of the main function. As noted, there are other errors, but placement is the cause of this one.
Was This Post Helpful? 0
  • +
  • -

#4 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Counting characters in a String using a function

Posted 04 June 2007 - 08:28 AM

I know that you are using your own function but an even easier way to do this would be to use the capabilities of the string class. You could let your function call the getline() function of the string class and then strip the spaces and then use the count function of the string class on the stripped string and store that in an integer value and return that integer value which would be the total number of characters in the line without white spaces.

This post has been edited by Topher84: 04 June 2007 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#5 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Counting characters in a String using a function

Posted 04 June 2007 - 08:38 AM

Hmm...if he were able to use the string class, he could also just use the .replace() method to get rid of the spaces, and the .length method to get that number of characters.
Was This Post Helpful? 0
  • +
  • -

#6 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Counting characters in a String using a function

Posted 04 June 2007 - 10:00 AM

Yes... sorry i meant length() not count.. sorry That would be the easiest way to do it though :) .. I don't have access to c++ here at work but I could possibly write it out later for you
Was This Post Helpful? 0
  • +
  • -

#7 summer291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 07

Re: Counting characters in a String using a function

Posted 04 June 2007 - 10:11 AM

View PostAmadeus, on 4 Jun, 2007 - 08:38 AM, said:

Hmm...if he were able to use the string class, he could also just use the .replace() method to get rid of the spaces, and the .length method to get that number of characters.




View PostAmadeus, on 4 Jun, 2007 - 08:38 AM, said:

Hmm...if he were able to use the string class, he could also just use the .replace() method to get rid of the spaces, and the .length method to get that number of characters.



Thanks all the help I could get would be appreciated.

This post has been edited by summer291: 04 June 2007 - 10:10 AM

Was This Post Helpful? 0
  • +
  • -

#8 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Counting characters in a String using a function

Posted 04 June 2007 - 10:35 AM

#include <iostream>
#include <string>	//Include this for the string class functionality

using namespace std;


int main()
{
  string strInput = "";	//String to hold line of text entered
  string strStripped = ""; //String to hold the stripped text
  int nNumItems = 0;	   //Int to hold the # of items in the string 

  cout << "Enter a line of words:" << endl; //Prompt user for text
  cin.getline(strInput,81); //Store text in strInput w/ a max size of 81

  while(blahblah)
  {
	 //Here you need to make an interator for strInput and iterate through the string 
	 //and use the .replace() functionality of the std::string to replace the spaces. You
	 //will do this with a while loop.. to do this you will set the iterator to the begining
	 //of the string and go from there ( gotta leave something for you to play with! ). Once
	 //the spaces are stripped you can assign the stripped string into strStripped and then store back
	 //into strInput ( or just go right to strInput )
  }//End While

  nNumItems = strInput.size();  //This will assign the number of colums associated with the string into nNumItems
								//I.E. the # of columns = the number of items in the stripped string.

  cout << "The number of items in the string is : " << nNumItems;   //Display to the user the number of items in the string
}//End Main()



This is the general idea of what you can do to make your life MUCH easier with your problem...

http://www.cpprefere...ring/index.html

that site should give more insight to the functionality of the string class... hope this helps you some

This post has been edited by Topher84: 04 June 2007 - 10:52 AM

Was This Post Helpful? 0
  • +
  • -

#9 cosette_hinds  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 30-December 06

Re: Counting characters in a String using a function

Posted 04 June 2007 - 04:50 PM

You could use the getline() function as this (as another option)

getline(cin, inputString); which will take text from the standard input stream until it reaches the default delimiter character (a newline character or carriage return from the keyboard).
Was This Post Helpful? 0
  • +
  • -

#10 Amadeus  Icon User is offline

  • g+ + -o drink whiskey.cpp
  • member icon

Reputation: 248
  • View blog
  • Posts: 13,506
  • Joined: 12-July 02

Re: Counting characters in a String using a function

Posted 05 June 2007 - 05:25 AM

I believe the getline() method is being used.
Was This Post Helpful? 0
  • +
  • -

#11 Topher84  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 113
  • View blog
  • Posts: 359
  • Joined: 04-June 07

Re: Counting characters in a String using a function

Posted 05 June 2007 - 05:31 AM

View Postcosette_hinds, on 4 Jun, 2007 - 04:50 PM, said:

You could use the getline() function as this (as another option)

getline(cin, inputString); which will take text from the standard input stream until it reaches the default delimiter character (a newline character or carriage return from the keyboard).


using a delimeter may be more work than what is needed because you would then have to create some form of a substring to hold the characters up to (and possibly including) the delimeter and then appened the strings to each other and THEN get the size/length of the string.
Was This Post Helpful? 0
  • +
  • -

#12 kiranvellanki  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 21-May 07

Re: Counting characters in a String using a function

Posted 05 June 2007 - 12:01 PM

Hi buddy maybe my code is use full for you…

#include<iostream>

using  namespace std;

int char_count(char str[100])
{
	int x;
	for(x=0;str[x]!='\0';x++);
	return x;
}

int main()
{
char a[100];
cout<<"Enter the String : ";
cin>>a;

cout<<"\n\nNumber of Char's in given string is "<<char_count(a);

return 0;
}


Output:

Enter the String : microsoft


Number of Char's in given string is 9
Was This Post Helpful? 0
  • +
  • -

#13 summer291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 07

Re: Counting characters in a String using a function

Posted 05 June 2007 - 12:20 PM

View Postkiranvellanki, on 5 Jun, 2007 - 12:01 PM, said:

Hi buddy maybe my code is use full for you…

#include<iostream>

using namespace std;

int char_count(char str[100])
{
int x;
for(x=0;str[x]!='\0';x++);
return x;
}

int main()
{
char a[100];
cout<<"Enter the String : ";
cin>>a;

cout<<"\n\nNumber of Char's in given string is "<<char_count(a);

return 0;
}

Output:

Enter the String : microsoft


Number of Char's in given string is 9



View Postsummer291, on 5 Jun, 2007 - 12:19 PM, said:

View Postkiranvellanki, on 5 Jun, 2007 - 12:01 PM, said:

Hi buddy maybe my code is use full for you…

#include<iostream>

using namespace std;

int char_count(char str[100])
{
int x;
for(x=0;str[x]!='\0';x++);
return x;
}

int main()
{
char a[100];
cout<<"Enter the String : ";
cin>>a;

cout<<"\n\nNumber of Char's in given string is "<<char_count(a);

return 0;
}

Output:

Enter the String : microsoft


Number of Char's in given string is 9


Thank you all so much! I love this site! It really helps me when trying to figure out what I need to do to fix errors and such.
Was This Post Helpful? 0
  • +
  • -

#14 summer291  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 29-April 07

Re: Counting characters in a String using a function

Posted 07 June 2007 - 09:43 AM

Thank you all so much. You all have really helped me. This site is very informational.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1