Jump to content

Get numbers from string. C

MisterWhite
Semifinals on October 24 th - 25 th at Expo, Place de Belgique 1, 1020 Ville de Bruxelles, BelgiumFinals on October 31 at Mercedes-Benz Arena Berlin, Platz 1, 10243 Berlin, Germany

Let's say we have this in text.txt. I can read the file like this:

FILE* stats = fopen ("text.txt", "r");while(!feof(stats) and !ferror(stats)){ fscanf(stats, "%s", &num);printf("%s\t", num);}

"num" will contain each word and number. The task is to extract all numbers from the text. Any ideas?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

you could split it for every word, then check each member of the gained array if its a number. that would be the most intuitive solution. now i assume this is java, so i cant tell you exactly how to do that, or if those numbers when split will automatically become integers or not, but that is just a small language thing to work out)

"Unofficially Official" Leading Scientific Research and Development Officer of the Official Star Citizen LTT Conglomerate | Reaper Squad, Idris Captain | 1x Aurora LN


Game developer, AI researcher, Developing the UOLTT mobile apps


G SIX [My Mac Pro G5 CaseMod Thread]

Link to comment
Share on other sites

Link to post
Share on other sites

you could split it for every word, then check each member of the gained array if its a number. that would be the most intuitive solution. now i assume this is java, so i cant tell you exactly how to do that, or if those numbers when split will automatically become integers or not, but that is just a small language thing to work out)

This is in C. Maybe you know how to check in C if it's a number?

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

C's string.h has a function called strtok which can split a string into tokens given a set of delimiters. If the input is exactly how you posted it you could split using the delimiters space and comma but you might need to get creative if you need a more general solution. It would be best to have a read of this http://www.cplusplus.com/reference/cstring/strtok/ to see how to use it because it's not very intuitive by modern programming standards.

 

Once you have the string tokenised into the individual words you can check if any of those words are integers using either isnumber (I couldn't find any documentation on this so I'm not sure if it's properly supported) or you could split each word into characters and call isdigit on each of them.

Link to comment
Share on other sites

Link to post
Share on other sites

This is in C. Maybe you know how to check in C if it's a number?

Well, in ASCII the numbers "0,1,2,3,4,5,6,7,8,9" are coded as "30,31,32,33,34,35,36,37,38,39"(ASCII) if that helps your case in any way.

http://linustechtips.com/main/topic/334934-unofficial-ltt-beginners-guide/ (by Minibois) and a few things that will make our community interaction more pleasent:
1. FOLLOW your own topics                                                                                2.Try to QUOTE people so we can read through things easier
3.Use
PCPARTPICKER.COM - easy and most importantly approved here        4.Mark your topics SOLVED if they are                                
Don't change a running system

Link to comment
Share on other sites

Link to post
Share on other sites

you can write your own function that will loop through every character and check if its one of digit character, if you want to validate signed integers then you need to check if there is '-' character and if it is on the very first position of string.

#include <stdio.h>#include <stdbool.h>#include <string.h>#include <stdlib.h>bool isNumber(const char *s){		size_t l = strlen(s);	if(l == 0) return false;		for(size_t i = 0; i < l; i++){		if((s[i] < '0' || s[i] > '9') && (s[i] != '-' || i > 0)){			return false;		}	}		return true;}int main(){	char text[80];		printf("Enter an integer number: ");	scanf("%s", &text);		if(isNumber(text)){		int number = atoi(text);		printf("%s converted to int = %d\n", text, number);			} else {		printf("%s is not an integer number.\n", text);	}		return 0;	}
And yeah, atoi is function to convert cstring to integer.
Link to comment
Share on other sites

Link to post
Share on other sites

you can write your own function that will loop through every character and check if its one of digit character, if you want to validate signed integers then you need to check if there is '-' character and if it is on the very first position of string.

#include <stdio.h>#include <stdbool.h>#include <string.h>#include <stdlib.h>bool isNumber(const char *s){		size_t l = strlen(s);	if(l == 0) return false;		for(size_t i = 0; i < l; i++){		if((s[i] < '0' || s[i] > '9') && (s[i] != '-' || i > 0)){			return false;		}	}		return true;}int main(){	char text[80];		printf("Enter an integer number: ");	scanf("%s", &text);		if(isNumber(text)){		int number = atoi(text);		printf("%s converted to int = %d\n", text, number);			} else {		printf("%s is not an integer number.\n", text);	}		return 0;	}
And yeah, atoi is function to convert cstring to integer.

I'll try it.

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×