Click here to Skip to main content
16,022,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
A very basic question.
(Iam using c.)

My Proglem:
-----------

I have a string containing unknown data, i have a function whish takes out the first "word" and i want to check if this word is a number or something else. How do i do?
Posted

 
Share this answer
 
Comments
andrWest 26-May-11 11:53am    
is digit takes a integer and i have a string.
Stephen Wiria 26-May-11 12:01pm    
Can't you do a loop for every char in your "word" and check it with isdigit?
After that you only need to atoi that "word".
andrWest 26-May-11 12:31pm    
Your right, thanks.
You should tokenize the string, in order to do that you can use the function strtok.

As you already have tokenized the string then you should use isalpha or isdigit to check each char inside the string...

Hope this helps!
 
Share this answer
 
v2
Comments
andrWest 26-May-11 11:55am    
i use a scannerADT, scanner datatype so i can easy get the first word in the string. but my problem is to check if this "word" is a aplha or digits
Joan M 26-May-11 12:00pm    
Improved the solution, let's see if it works for you now... ;)
andrWest 26-May-11 12:13pm    
Thanks ;), I did struggle abit with the ASCII table at first but the isdigit was a much better idea :)
Here is my C code,

Thanks for your support! :)


C#
bool stringIsDigit(string s){
    int length, i;
    char letter;
    length = StringLength(s);
    for(i=0;i<length;i++){
        letter = s[i];
        if(!isdigit(letter)){
            return FALSE;
        }
    }
}
 
Share this answer
 
v3
try this:

C++
#include <stdio.h>


C++
float f=0;
int n = sscanf("2345test", "%f", &f);


The integer returned by scanf(...) family is the number of successfully converted values. In this case it would be 1 because 2345 can be converted into a float.

If something is entered that cannot be converted into a floating point number the result will be 0.

scanf(...) is very "multi-purpose" - it is possible to scan for integers and strings, too. So you should check the documentation on it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900