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