PDA

View Full Version : c++




cades
Apr 13, 2006, 05:46 AM
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..:)



Wes
Apr 13, 2006, 06:15 AM
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...

Soulstorm
Apr 14, 2006, 02:35 AM
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";
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

AlmostThere
Apr 14, 2006, 10:13 AM
#include <cctype>

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

darkwing
Apr 14, 2006, 10:32 AM
#include <cctype>

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

You missed the first character. Use i++.

AlmostThere
Apr 14, 2006, 11:17 AM
No, I didn't.

Soulstorm
Apr 14, 2006, 03:39 PM
#include <cctype>

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

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

darkwing
Apr 14, 2006, 04:31 PM
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
Apr 14, 2006, 04:31 PM
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. :)

savar
Apr 14, 2006, 05:21 PM
His problem was to count the number of numerical characters, not the number of of numbers. :)

Understanding the problem: 90% of programming.

AlmostThere
Apr 14, 2006, 07:10 PM
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. :)

Better now than in a month's time :)

darkwing
Apr 14, 2006, 08:11 PM
Better now than in a month's time :)

Don't remind me.

Soulstorm
Apr 15, 2006, 06:30 AM
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
Apr 15, 2006, 12:40 PM
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))
numDigits++;

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]))
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
Apr 20, 2006, 06:27 PM
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
Apr 21, 2006, 01:58 AM
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.