Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,138 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,062 people online right now. Registration is fast and FREE... Join Now!




Counting characters in a String using a function

2 Pages V  1 2 >  
Reply to this topicStart new topic

Counting characters in a String using a function

summer291
3 Jun, 2007 - 05:06 AM
Post #1

New D.I.C Head
*

Joined: 29 Apr, 2007
Posts: 8


My Contributions
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.


CODE
#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;
}

User is offlineProfile CardPM
+Quote Post

v0id
RE: Counting Characters In A String Using A Function
3 Jun, 2007 - 06:26 AM
Post #2

New D.I.C Head
*

Joined: 22 Nov, 2006
Posts: 4


My Contributions
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.
CODE

int Char_Count(char *szStr)
{
    int iCount = 0;
    while(*szStr++ != '\0') iCount++;
    return iCount;
}


This post has been edited by v0id: 3 Jun, 2007 - 06:27 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Counting Characters In A String Using A Function
3 Jun, 2007 - 12:50 PM
Post #3

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Topher84
RE: Counting Characters In A String Using A Function
4 Jun, 2007 - 07:28 AM
Post #4

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


Dream Kudos: 25
My Contributions
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: 4 Jun, 2007 - 07:30 AM
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Counting Characters In A String Using A Function
4 Jun, 2007 - 07:38 AM
Post #5

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

Topher84
RE: Counting Characters In A String Using A Function
4 Jun, 2007 - 09:00 AM
Post #6

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


Dream Kudos: 25
My Contributions
Yes... sorry i meant length() not count.. sorry That would be the easiest way to do it though smile.gif .. I don't have access to c++ here at work but I could possibly write it out later for you
User is offlineProfile CardPM
+Quote Post

summer291
RE: Counting Characters In A String Using A Function
4 Jun, 2007 - 09:11 AM
Post #7

New D.I.C Head
*

Joined: 29 Apr, 2007
Posts: 8


My Contributions
QUOTE(Amadeus @ 4 Jun, 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.




QUOTE(Amadeus @ 4 Jun, 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.



Thanks all the help I could get would be appreciated.

This post has been edited by summer291: 4 Jun, 2007 - 09:10 AM
User is offlineProfile CardPM
+Quote Post

Topher84
RE: Counting Characters In A String Using A Function
4 Jun, 2007 - 09:35 AM
Post #8

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


Dream Kudos: 25
My Contributions
CODE

#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.cppreference.com/cppstring/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: 4 Jun, 2007 - 09:52 AM
User is offlineProfile CardPM
+Quote Post

cosette_hinds
RE: Counting Characters In A String Using A Function
4 Jun, 2007 - 03:50 PM
Post #9

New D.I.C Head
*

Joined: 30 Dec, 2006
Posts: 22


My Contributions
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).


User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Counting Characters In A String Using A Function
5 Jun, 2007 - 04:25 AM
Post #10

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
I believe the getline() method is being used.
User is offlineProfile CardPM
+Quote Post

Topher84
RE: Counting Characters In A String Using A Function
5 Jun, 2007 - 04:31 AM
Post #11

D.I.C Head
Group Icon

Joined: 4 Jun, 2007
Posts: 232


Dream Kudos: 25
My Contributions
QUOTE(cosette_hinds @ 4 Jun, 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).


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.
User is offlineProfile CardPM
+Quote Post

kiranvellanki
RE: Counting Characters In A String Using A Function
5 Jun, 2007 - 11:01 AM
Post #12

New D.I.C Head
*

Joined: 21 May, 2007
Posts: 6


My Contributions
Hi buddy maybe my code is use full for you…

CODE
#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
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 12/1/08 10:41PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month