string from vector and seperate in to char?

Is there a way to grab a string from a string vector and split it in to individual character so i can check weather they are numbers or letters?

I want to do this so i can get a string like "12as 32312dsda sda23asd" and turn it in to individual strings in a vector and then check weather its a number or letter, then add the 3 numbers or more up. like 12 +32312 + 23=.....

so far i just have the string separated in to separate strings in a vector.

[code]

void readData(ifstream& in, double& sum, int& count)
{
double tempSum = double();
string line;
string word;
int score;
int length;
int i = 0;
int k = 0;


getline(in, line); //takes line from in and puts in string line

istringstream iss(line); //takes whole line and turns in to indv strings


vector< string > words((istream_iterator<string>(iss)), istream_iterator<string>());
while (i < words.size())
{
cout << words[i]; //print vector element
i++;
}
}

I can parse a normal string and see if the char is a digit with something like:
int i = 0;
if(i < line.length())
{

if (isdigit(line[i]))
{
cout << line[i] << endl;

i++;
}
}

but how do i call a string from a vector and preform the same check and do it till i read all the words from the string?
Last edited on
I want to do this so i can get a string like "12as 32312dsda sda23asd" and turn it in to individual strings in a vector and then check weather its a number or letter, then add the 3 numbers or more up. like 12 + 32312 + 23=.....

In other words, can you tell us what this string will become :
"12as 32312dsda sda23asd"?
Last edited on
basically i split "12as 32312dsda sda23asd" in to separate string words: "12as" "32312dsda" "sda23asd" and put them in a vector. I am asking how i call the data from the vector, split each letter from the word string and then test each letter to see if the letter is a number and if it is hold it and after it found all the numbers in the word string it would combine them. like the word string 12as would become the int 12, or sda23asd would become 23.
In other words, you want to extract the integers from the strings?
extract the integers in the strings from the vector, yes.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	int i;
	int testNumber;
	char testChar;
	string input;
	stringstream ss;
	vector<string> myStrings;
	vector<int> myIntegers;

	cout << "Enter the string : "; getline(cin, input);

	ss << input;
	while(ss >> input) myStrings.push_back(input);

	for(i = 0; i < myStrings.size(); i++)
	{
		ss.str("");
		ss.clear();
		ss << myStrings[i];

		bool bQuit = false;
		while(bQuit == false)
		{
			if(ss >> testNumber) 
			{
				myIntegers.push_back(testNumber);
				bQuit = true;
			}
			else
			{
				ss.clear();
				if(!(ss >> testChar)) bQuit = true;
			}
		}
	}
	

	cout << "The numbers in the strings : " << endl;
	for(i = 0; i < myIntegers.size(); i++)
	{
		cout << myIntegers[i] << endl;
	}

	cin.get();
	return 0;
}


Enter the string : 12as 32312dsda sda23asd

The numbers in the strings :
12
32312
23
Last edited on
Can you explain to me which part splits the string in to individual chars, to be checked if it is a number? Or how this works?
Last edited on
Topic archived. No new replies allowed.