Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cades

macrumors newbie
Original poster
Apr 12, 2006
1
0
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..:)
 

4409723

Suspended
Jun 22, 2001
2,221
0
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.

Code:
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...
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
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...
Code:
#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";
	if(checkNumber(token)){
		cout << "isnumber! " << checkNumber(token) << "\n";
		v.push_back(checkNumber(token));
	}
		
	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";	
	check(p);
	
	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
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
AlmostThere said:
#include <cctype>

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


You missed the first character. Use i++.
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
AlmostThere said:
#include <cctype>

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


yes, but what happens when the number is two digit?
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
AlmostThere said:
No, I didn't.

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. :)
 

darkwing

macrumors 65816
Jan 6, 2004
1,210
0
Soulstorm said:
yes, but what happens when the number is two digit?

His problem was to count the number of numerical characters, not the number of of numbers. :)
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
is problem was to count the number of numerical characters, not the number of of numbers.
damn. You're right. My brain was burnt after too much effort of trying to learn OBJ-C.
 

Rye Brye

macrumors newbie
Oct 20, 2003
18
0
Utah
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.

Code:
 #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))
numDigits++;

if you have a string and you want to check against an std::string object...
(assuming the string is named "inputStr"):
Code:
for(unsigned int i = 0; i < inputStr.size(); i++) {
    if (isdigit(inputStr[i]))
        numDigits++;
}

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

MarkCollette

macrumors 68000
Mar 6, 2003
1,559
36
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?
 

Soulstorm

macrumors 68000
Feb 1, 2005
1,887
1
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.