Reputation:
Hello I would like to check for a float number in string
#include<stdio.h>
#include <math.h>
#include <ctype.h>
#include <stdlib.h>
int main(void)
{
char qihjuq[] = "a s d a s d g g 1 2 3 1 2 3 5 5.4 d 10.4";
int num[256];
float digit[256];
char let[256] = {"0"};
int lcounter =0;
int ncounter =0;
int dcounter =0;
for(int i =0; i< sizeof qihjuq; i++)
{
if(isalpha(*(qihjuq+i))) {
let[lcounter] = *(qihjuq + i);
lcounter++;
}
else if(isdigit(*(qihjuq+i)) && *(qihjuq+i+1) != '.') {
num[ncounter] = *(qihjuq + i);
ncounter++;
}
else if(roundf(*(qihjuq+i)) != *(qihjuq+i)) {
digit[dcounter] = *(qihjuq + i);
dcounter++;
}
}
printf("The letters are: \n");
for(int i =0; i< lcounter; i++)
printf("%c ",let[i]);
printf("The whole numbers are: \n");
for(int i =0; i< ncounter; i++)
printf("%c ",num[i]);
}
The problem lies in the second and third if
else if(isdigit(*(qihjuq+i)) && *(qihjuq+i+1) != '.') {
num[ncounter] = *(qihjuq + i);
ncounter++;
}
else if(roundf(*(qihjuq+i)) != *(qihjuq+i)) {
digit[dcounter] = *(qihjuq + i);
dcounter++;
}
where the program has to detect whether the is float or integer. The problem is that the program is detecting 10.4 as individual characters which in turn puts 1 0 4 in the whole number to try to negate this I added a detection for . but still has logical errors since the condition does not include the numbers behind the .
Upvotes: 1
Views: 227
Reputation: 40145
like this:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(void){
char qihjuq[] = "a s d a s d g g 1 2 3 1 2 3 5 5.4 d 10.4";
int num[256];
float digit[256];
char let[256];
int lcounter =0;
int ncounter =0;
int dcounter =0;
for(int i = 0; i < sizeof(qihjuq) -1; i++){
if(isspace(qihjuq[i]))
continue;//skip spaces
char work[256] = {0};
for(int j = 0; qihjuq[i] && !isspace(qihjuq[i]); ++j, ++i)
work[j] = qihjuq[i];//Extraction
--i;//for next loop
if(isalpha(*work) && !work[1]) {//one letter
let[lcounter++] = *work;
} else {
char *p = work;
int n = strtol(p, &p, 10);
if(!*p)//convert to int succeeded (Not strict)
num[ncounter++] = n;
else {
p = work;
float f = strtod(p, &p);
if(!*p)//convert to float succeeded (Not strict)
digit[dcounter++] = f;
}
}
}
printf("The letters are: \n");
for(int i = 0; i < lcounter; i++)
printf("%c ", let[i]);
printf("\nThe whole numbers are: \n");
for(int i = 0; i < ncounter; i++)
printf("%d ", num[i]);
puts("");
}
Upvotes: 1
Reputation: 108978
Just read a double
with strtod()
.
If it works, the end
pointer points to the position to read from; if it doesn't work advance by 1.
Keep at it until the end of the string
/* code untested */
double x;
char *end
char qihjuq[] = "a s d a s d g g 1 2 3 1 2 3 5 5.4 d 10.4";
char *p = qihjuq;
while (*p) {
errno = 0;
x = strtod(p, &end);
if (errno) {
p += 1;
} else {
printf("found %f\n", x);
p = end;
}
}
Upvotes: 0
Reputation: 16660
I do not understand your approach.
You have – let's call it – tokens in a string separated by spaces. You can neither detect the kind of a token nor the value of that token by simply looking on one character.
You have two options: (Logically they are the same, but the implementation is different.)
You iterate over the string extracting the tokens and then iterate over each token to detect its kind.
You iterate over the string and recursively iterate over a token.
Like so (typped in browser)
char *char_ptr = qihjuq;
do
{
if( char_ptr == ' ')
{
continue
}
// iterate overe the next token
int char_set = 0x0;
char * start_ptr = char_ptr; // The start of an item
do
{
if( isdigit( *char_ptr) )
{
char_set |= 0x01; // contains digit
}
else if( *char_ptr == '.' )
{
char_set |= 0x02; // contains period
}
else if( isalpha( *char_ptr ))
{
char_set |= 0x04;
}
break; // any other character breaks item
} while( *char_ptr++ );
// integers have 0x01, floats have 0x3. Other values are alphas or alphanumerics
// you can store the result into an array, do some processing with them or whatever you want. start_ptr points to the beginning of the item_char_ptr to one char after the end.
} while ( *char_ptr++ )
Likely there are some bugs, but this is the structure.
Upvotes: 0