Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to double a float/integer within a user input string in C?

Tags:

c

I'm sorry in advance because I'm fairly new to programming and some things in my code will probably look like utter nonsense! I'm not entirely sure if I'm using atoi right.

I'm trying to create a program that splits a user input sentence into single words and doubles the number(float/integer) if a user inputs one. For example, I have 3 cats would come out as:

I
have
6
cats

My program right now is able to split the sentence, but I can't get the integer to double. Can anyone help me with this?

Here is the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void main()
{
    char sentence[100];

    printf("Enter a sentence to split: ");
    scanf("%[^\n]s", sentence);
    char *pch;
    int y;

    y = atoi(sentence);
    printf("After splitting:\n", sentence);
    pch = strtok(sentence," ");
    while (pch != NULL) {
        printf("%s\n", pch);
        pch = strtok(NULL, " ");
    }
    system("PAUSE");
}

And my output so far:

Enter a sentence to split: Hi, I have 7 cats.
After splitting:
Hi,
I
have
7
cats.
Press any key to continue . . .
like image 660
Bluth Avatar asked Mar 14 '26 12:03

Bluth


1 Answers

Here is a simpler version with a test for all digit numbers:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void) {
     char sentence[100];
     char *pch;

     printf("Enter a sentence to split: ");
     if (!fgets(sentence, sizeof sentence, stdin))
          return 1;

     printf("After splitting:\n");
     for (pch = strtok(sentence, " \n"); pch != NULL; pch = strtok(NULL, " \n")) {
         if (pch[strspn(pch, "0123456789")] == '\0') {
             printf("%d\n", atoi(pch) * 2);
         } else {
             printf("%s\n", pch);
         }
     }
     system("PAUSE");
     return 0;
}

If you want to parse floating point numbers too, you could use this code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>

int main(void) {
     char sentence[100];
     char *pch, *pend;
     double value;

     printf("Enter a sentence to split: ");
     if (!fgets(sentence, sizeof sentence, stdin))
          return 1;

     printf("After splitting:\n");
     for (pch = strtok(sentence, " \n"); pch != NULL; pch = strtok(NULL, " \n")) {
         value = strtod(pch, &pend);
         if (*pend == '\0' && isfinite(value)) {
             printf("%g\n", value * 2);
         } else {
             printf("%s\n", pch);
         }
     }
     system("PAUSE");
     return 0;
}

Note the test for isfinite() to avoid recognizing inf and nan as numbers.

NOTE: isfinite is part of C99, it is not supported by VisualStudio 12, but more recent versions do support it. For this older version, use _finite() defined in <float.h>.

like image 63
chqrlie Avatar answered Mar 17 '26 02:03

chqrlie



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!