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

Bluth

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 . . .
chqrlie

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>.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to check data type of user input like: int, double and string in c

From Dev

Clojure casting string to double from user input

From Dev

User input sorted into two arrays, double and string

From Dev

C# Dynamically parse user input to string, int, double, decimal, datetime, bool, etc

From Dev

User input string variable in C

From Dev

How to store user string input in array, C++

From Dev

How to compare user input with string?

From Dev

using exception to store user input as either double or string

From Dev

Ask user to input a double instead of a String without making the program shutdown

From Dev

C - how to delete string the same string as user input from a file in c?

From Dev

C++ List within Class to store user input

From Dev

C++ String user input into Class Object?

From Dev

Count words in a user-input string in C

From Dev

Write user input string to file in C

From Dev

Count words in a user-input string in C

From Dev

Append to C String Based on User Input

From Dev

User string input to go until \0 in c

From Dev

String concatenation with user input in C++

From Dev

How to split string by comma within parenthesis but not those on double quote

From Dev

How to interpolate ${:-=cat} within a double-quoted string?

From Dev

How to Deal double quotes and commas within csv string?

From Dev

Drawing a Diamond with User Input (not double of the user input)

From Dev

How to get a string from an input string defined within a frame with regex

From Dev

How to update database records within a user input range?

From Dev

How to provide user input to a java application invoked within a shell script?

From Dev

C/C++ , How to deny inputting char/string when waiting for the user to input a int , float or whatsoever?

From Dev

In javascript, how to manipulate a multiline string input by user?

From Dev

How to convert user-input into string

From Dev

How to limit user input to certain string values?

Related Related

  1. 1

    How to check data type of user input like: int, double and string in c

  2. 2

    Clojure casting string to double from user input

  3. 3

    User input sorted into two arrays, double and string

  4. 4

    C# Dynamically parse user input to string, int, double, decimal, datetime, bool, etc

  5. 5

    User input string variable in C

  6. 6

    How to store user string input in array, C++

  7. 7

    How to compare user input with string?

  8. 8

    using exception to store user input as either double or string

  9. 9

    Ask user to input a double instead of a String without making the program shutdown

  10. 10

    C - how to delete string the same string as user input from a file in c?

  11. 11

    C++ List within Class to store user input

  12. 12

    C++ String user input into Class Object?

  13. 13

    Count words in a user-input string in C

  14. 14

    Write user input string to file in C

  15. 15

    Count words in a user-input string in C

  16. 16

    Append to C String Based on User Input

  17. 17

    User string input to go until \0 in c

  18. 18

    String concatenation with user input in C++

  19. 19

    How to split string by comma within parenthesis but not those on double quote

  20. 20

    How to interpolate ${:-=cat} within a double-quoted string?

  21. 21

    How to Deal double quotes and commas within csv string?

  22. 22

    Drawing a Diamond with User Input (not double of the user input)

  23. 23

    How to get a string from an input string defined within a frame with regex

  24. 24

    How to update database records within a user input range?

  25. 25

    How to provide user input to a java application invoked within a shell script?

  26. 26

    C/C++ , How to deny inputting char/string when waiting for the user to input a int , float or whatsoever?

  27. 27

    In javascript, how to manipulate a multiline string input by user?

  28. 28

    How to convert user-input into string

  29. 29

    How to limit user input to certain string values?

HotTag

Archive