Short question abous strings and numbers

Hello I have txt file
Hello, my name is Brian. I like to tra00123456789vel my phone number is 65254875245652 ok, tha156845ts not my tr5632e number

And I need to find only those words which contains only numbers f.ex 65254875245652, but tra00123456789vel is not correct becouse word contain numbers and words...
Here is my code

1
2
3
4
5
6
7
8
9
bool FindOnlyNumbersInMyTXTFile(string E){ // E - line
	string sk = "0123456789";
	for(int i=0; i<E.length(); i++){
		if(sk.find(E[i])){
			return true;
		}
	}
	return false;
}


Thanks for reply ;)
Last edited on
?
I know that thereare a function isaplpha, but it wont work for me. What's wrong? :/

#include <stdio.h>
#include <ctype.h>

1
2
3
4
5
6
7
8
9
bool FindOnlyNumbersInMyTXTFile(string E){ // E - line
	string sk = "0123456789";
	for(int i=0; i<E.length(); i++){
		if(sk.find(E[i]) && !isalpha(E[i])){
			return true;
		}
	}
	return false;
}
Any help?
2 problems:

1. You are searching the entire line at once
2. once you find even a single number on the line you return true.

Try and break the line into words, use the std::isdigit to see if every character in the word is a digit. Here's a quick old-timey example, you should use more modern C++ features in your own implementation.
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
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

// really old way, also doesn't deal with decimal points, signs 
// or  number formats other than straightforward integral values
bool is_number(const string &word)
{
	for (size_t i = 0; i < word.size(); ++i)
	{
		if (!isdigit(word[i]))
		{
			return false;
		}
	}
	
	return true;
}

int main()
{
	string line("Hello, my name is Brian. I like to tra00123456789vel my phone number is 65254875245652 ok, tha156845ts not my tr5632e number");
	
	istringstream iss(line);
	string word;
	while (iss >> word)
	{
		if (is_number(word))
		{
			cout << word << endl;
		}
	}
	
	return 0;
}


output is
65254875245652
Thanks, but this way is the onlly way? maybe there are more simpliar and shorter way? thanks
Here's how i try to think
1
2
3
4
5
6
7
8
9
10
11
bool TikSkaitmenys(string E){
	string sk = "0123456789";
	for(int i = 0; i < E.length(); i++){
		for(char j = 'a'; j < 'z'; j++){
			if(E[i] != j){
				return true;
			}
		}
	}
	return false;
}

but it still wont work, i dont know what to do...
There are indeed many, many ways :)
tipaye, could you show me the easiest and the shortest? thanks
Last edited on
It depends on what appeals to you the most. I must point out that the functions you're writing are neither as short nor as simple as the example I used.

Your last one won't work of course. It just doesn't do any of the things you're tying to.

Why don't you want to use isdigit()? The function is_number() does exactly what you want, in 3 statements, it is already very simple and very short. I suppose it could be shortened to 2 lines, but that reduces clarity and improves nothing.

Also show your full code, including main() and the #includes. Be very clear about what the problem is. It's just occurred to me that you've not actually stated what the problem is, and I'm beginning to wonder what's going on in the surrounding code.
ok tipaye
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include <regex>

using namespace std;

const string CD = "Tekstas.txt";
const string RF = "RedTekstas.txt";
const string skyr = " .,";

bool TikSkaitmenys(string E);
int SkaitmenuSuma(string E);
void RastiZodiEil(string E, string Sk, string & Z);

int main(){
	setlocale(LC_ALL, "Lithuanian");

	string eil;
	string Z;

	ifstream fd(CD);
	ofstream rf(RF);

	while(!fd.eof()){
		getline(fd, eil);
		RastiZodiEil(eil, skyr, Z);
	}
	fd.close();
	rf.close();

	system("pause");
	return 0;
}

bool TikSkaitmenys(string E){
	string sk = "0123456789";
	for(int i=0; i<E.length(); i++){
		if(sk.find(E[i])){
			return true;
		}
	}
	return false;
}

int SkaitmenuSuma(string E){
	int sum = 0;
	for(int i=0; i<E.length(); i++){
		if(TikSkaitmenys(E)){
			sum = sum + E.length();
		}
	}
	return sum;
}

void RastiZodiEil(string E, string Sk, string & Z){
	Z = "";

	size_t Pr = E.find_first_not_of(Sk, 0);
	size_t Pb = E.find_first_of(Sk, Pr);

	while(Pr != string::npos && E.length()){
		string laik = E.substr(Pr, Pb - Pr);
		if(laik.length() > Z.length() && TikSkaitmenys(E)){
			Z = laik;
		}
		Pr = E.find_first_not_of(Sk, Pb);
		Pb = E.find_first_of(Sk, Pr);
	}
	cout << Z << endl;
}



And ifI have a word hi555my name is 86456

so my results should be 86456
Last edited on
Hi,
I'll just put up another example, based on my interpretation of what you're trying to do.

I've modified the code you gave above. I assume you have a file called "Tekstas.txt" in the current folder when running. "RedTekstas.txt" Will be created if it doesn't already exist.

I've mady very lazy changes (only 24 hours in a day :-)), but please pay attention to the changes, and carefully compare with the former code.

You should see the proper output on cout and also in RedTekstas.txt.

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
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <fstream>
#include <string>
#include <cstring>

using namespace std;

void RastiZodiEil(const string &E, const string &Sk, ofstream &rf);
bool TikSkaitmenys(const string &E);

int main()
{
	setlocale(LC_ALL, "Lithuanian");
	
	string eil;
	const string skyr = " .,";
	ifstream fd("Tekstas.txt");
	ofstream rf("RedTekstas.txt");
	
	while(!fd.eof()){
		getline(fd, eil);
		RastiZodiEil(eil, skyr, rf);
	}
	fd.close();
	rf.close();
	
	cin.get();
	return 0;
}

void RastiZodiEil(const string &E, const string &Sk, ofstream &rf)
{

	char *c_string = new char[E.size()];
	strcpy(c_string, E.c_str());

	char *pch;
	pch = strtok(c_string, Sk.c_str());
	while (pch != 0)
	{
		if (TikSkaitmenys(pch))
		{
			cout << pch << endl;
			rf << pch << endl;
		}
		
		pch = strtok(0, Sk.c_str());
	}
}

bool TikSkaitmenys(const string &E)
{
	for (size_t i = 0; i < E.size(); ++i)
	{
		if (!isdigit(E[i]))
		{
			return false;
		}
	}
	
	return true;
}
Topic archived. No new replies allowed.