Discussion in 'Mac Programming' started by cades, Apr 13, 2006.

  1. cades macrumors newbie

    Apr 12, 2006
    i have face a problem to creat a c++ program..
    to count the number of numerical character in a sentences that have been key in by user..:)
  2. 4409723 Suspended


    Jun 22, 2001
    I don't know c++ but here's a way you could do it in Java, for such a simple program it should be quite easy to see how to do in c++ providing you know the syntax.

    String sentence = "blah blah blah"; //Assuming you've read in the sentence.
    char c = 'a'; //Character you are looking for.
    int counter = 0; //Number of occurences of this character so far.
    for (int i = 0; i < sentence.length(); i++) {
         if (sentence.charAt(i) == c) {counter++;}
    System.out.println("The number of occurrences of '" + c + "' in the string is: " + counter + ".");
    Only slightly annoyance is the way indices are defined. The first item of the string is give an index of '0', the second '1' and so on. That's why the for loop starts at 0 and doesn't ever reach sentence.length(), as that could lead to troubles with index out of bounds.

    Hope that helps a little...
  3. Soulstorm macrumors 68000


    Feb 1, 2005
    you need to do some work here.

    Lets say you have an array of 30 chars which holds the string. Check the array piece by piece for any number. the first number you encounter, convert it to a number using "atoi()" or "atof()" in a variable. After continuing checking the array, if the program encounters a number next to the last one, it will convert it to a number and it will add it to the variable.

    and so on...

    For the float numbers, you will need to also check each "." character and do the appropriate functions.

    I am not in front of xcode now, so, I will get back to you when i have the code.

    EDIT: I just thought something simpler. You can just check your sentence for two spaces (isolate each word and see if it is integer or float).

    I think I have some code ready...
    #include <iostream>
    #include <vector>
    using namespace std;
    vector<float> v;
    float checkNumber(char *p){
    	return atof(p);
    void check(char *str){
    	int length = strlen(str);
    	int i,j;
    	char token[length];
    	for(i=0; ; i++){
    		for(j=0; (str[i]!=' ' && str[i]); j++, i++)
    			token[j] = str[i];
    	token[j] = '\0';
    	cout << token << "\n";
    		cout << "isnumber! " << checkNumber(token) << "\n";
    	if(!str[i]) break;
    int main (int argc, char * const argv[]) {
    	char p[] = "95 he 89 llo 12.54 helllo again 1254 3894 ok 90";	
    	cout << "vector contents:\n";
    	for(int i=0; i<v.size(); i++){
    		cout << v[i] << "\n";
    	return 0;
    The only problem is that it doesn't recognize the number 0; But I think it can easily be fixed with an 'if' statement... It would be easier to do it with the use of strings, but I figured you wanted it to use it with arrays...

    EDIT: It will recognize numbers no matter what type there are, float, integers, and no matter how many digits they contain. try it
  4. AlmostThere macrumors 6502a

    #include <cctype>

    for (unsigned i = 0; i<inputString.length(); ++i)
    if (isdigit(inputString)) counter++;
  5. darkwing macrumors 65816

    Jan 6, 2004

    You missed the first character. Use i++.
  6. Soulstorm macrumors 68000


    Feb 1, 2005

    yes, but what happens when the number is two digit?
  7. darkwing macrumors 65816

    Jan 6, 2004
    You're right. Wasn't thinking. Too much studying for thecomprehensive exam next month. Wrote implementations of merge/quick/heap sorts the other day to make sure I knew them. Brain fried. :)
  8. darkwing macrumors 65816

    Jan 6, 2004
    His problem was to count the number of numerical characters, not the number of of numbers. :)
  9. savar macrumors 68000


    Jun 6, 2003
    District of Columbia
    Understanding the problem: 90% of programming.
  10. AlmostThere macrumors 6502a

    Better now than in a month's time :)
  11. darkwing macrumors 65816

    Jan 6, 2004
    Don't remind me.
  12. Soulstorm macrumors 68000


    Feb 1, 2005
    damn. You're right. My brain was burnt after too much effort of trying to learn OBJ-C.
  13. Rye Brye macrumors newbie

    Oct 20, 2003
    A simpler solution

    You say it is for input keyed in by the user...

    How are you getting your input?

    Are you reading it in from cin << ? Are you asking for a function that will get the number of numeric characters from the input stream?

    Elaborate on your input and I can give you a really easy way to count the number of numerical characters in it.

     #include <ctype.h>
    If you are reading the input in character by character - it's as simple as this:
    ( assuming you read in to char c):

    if (isdigit(c))

    if you have a string and you want to check against an std::string object...
    (assuming the string is named "inputStr"):
    for(unsigned int i = 0; i < inputStr.size(); i++) {
        if (isdigit(inputStr[i]))
    There are certainly a lot more elaborate ways that you can do it - but from what it sounds like you just need to know the number of digits inputted by the user.

    Now, if you are talking about parsing input strings like "one" or "two" or "three" and turning them into 1, 2, 3 - as numeric values - that's a different ballgame entirely.

    edit: I just scrolled up further and realized someone else posted about the same thing.... so this post was unncessary...
  14. MarkCollette macrumors 68000


    Mar 6, 2003
    Toronto, Canada
    Hmm, almost a week later, and we still haven't been told if "numerical character" refers to either digits, chars, or ascii string representations of numbers which may be integral or fractional. And if it's the latter, is scientific notation or engineering notation supported? What about infinity, NaN, etc. Should it accept only ascii, or UTF-8, or fixed length unicode? What about numerical symbols in unicode, like the symbol for 1/2, etc... What numerical base are the numbers in, base 10, or binary, octal, hexadecimal, arbitrary?
  15. Soulstorm macrumors 68000


    Feb 1, 2005
    Because of the author of the thread not responding back, I think it would be right to assume that his question was answered... so the answer you are looking for is that the thread probably concerned one-digit numerical character.

Share This Page