Bluth
Bluth

Reputation: 33

How to double a float/integer within a user input string in 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 . . .

Upvotes: 3

Views: 76

Answers (4)

chqrlie
chqrlie

Reputation: 145297

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

Upvotes: 1

bpm
bpm

Reputation: 105

Here's another (more compact) answer:

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

int is_numeric(char *s)
{
    int i = 0;
    while (s[i] != '\0') {
        if (!isdigit(s[i]))
            return 0;
        ++i;
    }
    return 1;
}

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

    printf("Enter a sentence to split: ");
    if (!fgets(sentence, sizeof(sentence), stdin)) {
        return 1;
    }
    sentence[strcspn(sentence, "\n\r")] = 0; /* Strip newline */

    pch = strtok(sentence, " ");
    while (pch != NULL) {
        if (atoi(pch)) {
            printf("%d\n", 2 * atoi(pch));
        } else {
            printf("%s\n", pch);
        }
        pch = strtok(NULL, " ");
    }
    return 0;
    /* system("PAUSE"); */
}

The key is that atoi will return 0 for a non-numeric argument.

Upvotes: 0

Simply Me
Simply Me

Reputation: 1587

For the split part I would recommend this version, it is easier to understand (considering that you've learned the for way of working). It does the same thing, but helps you organize your code.

char *p;
int i;
    for(p = strtok(sentence, " "); p != NULL; p = strtok(NULL, " "))
    {
        int isNumber = 1;
        for(i = 0; i < strlen(p); i ++)
        {
            if(!isDigit(p[i])
            {
                isNumber = 0;
                break;
            }
        }
        if(isNumber == 1)
        {
            int number = atoi(pch);
            number *= 2;
            printf("%d\n", number);
        }
        else
        {
            printf("%s\n", p);
        }
    }

For the number, I would recommend using the atoi function. Here you have a good reference.

To solve your problem, first of all, you need to check every single word you get. For example: You take it word by word and see if the "word" contains ONLY digits. If it contains only digits, you can use atoi to convert it to a number, multiply it by 2 and print the result.

On the other hand, if you find a char that is NOT a digit, you have letters or any other characters in your word, so it is not a word so you simply print that as it is.

Upvotes: 0

Sossenbinder
Sossenbinder

Reputation: 5322

Here is a working example, even if it's not really precise. Basically, we need to find out whether our current string is a number or not. In this case, I just tried to get the first element of the current string, and tried to determine if it's numeric or not.

Of course, if you want to be really sure, we need to iterate the string and check every char, if it is a number or not.

#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)
    {
        if(isdigit(pch[0]))
        {
            int number = atoi(pch);
            number *=2;
            printf("%d\n",number);
            pch = strtok (NULL, " ");
            continue;
        }
        printf("%s\n",pch);
        pch = strtok (NULL, " ");
    }

    system("PAUSE");
}

Upvotes: 0

Related Questions