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 + ".");
#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;
}
AlmostThere said:#include <cctype>
for (unsigned i = 0; i<inputString.length(); ++i)
if (isdigit(inputString)) counter++;
AlmostThere said:#include <cctype>
for (unsigned i = 0; i<inputString.length(); ++i)
if (isdigit(inputString)) counter++;
AlmostThere said:No, I didn't.
Soulstorm said:yes, but what happens when the number is two digit?
darkwing said:His problem was to count the number of numerical characters, not the number of of numbers.
darkwing said: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.
damn. You're right. My brain was burnt after too much effort of trying to learn OBJ-C.is problem was to count the number of numerical characters, not the number of of numbers.
#include <ctype.h>
for(unsigned int i = 0; i < inputStr.size(); i++) {
if (isdigit(inputStr[i]))
numDigits++;
}