# Thread: Splitting Digits and Letters

1. ## Splitting Digits and Letters

Does anybody know how to split up digits so that i can manipulate them?

eg.. for example the user types 123, i want to split them up and add them to get 6. Also wondering how to split up words??

this is a sample sentence

how do i break the above apart so that i can count that there are 5 words and however many letters there?

??

2. My only suggestion would be to have some sort of an array that stores the input and a counter that goes thru the inputted string and checks to see what is entered. If it is a number have a counter for a number or a word Have different conditions to check for like space. Once u come across something just store it in the array increment the counter and oprerate on the input from there

3. for words look up strtok(). Its been discussed many times on this board. use the search facilities provided.

for ints.
Code:
```#include<stdio.h>

int main()
{
int sum=0;
int wotsit=0;
int i=0;
char doodah[20]={0};
printf("enter wotsit?");
scanf("%d",&wotsit);
sprintf(doodah,"%s",wotsit); // write int into memory as a string
while (doodah[i] != '\0')
{
sum+= (doodah[i]-'0');
++i;
}
printf("\nSum of digits is :-%d",sum);
return 0;
}```

4. Ya know, code tags are nice, but you actually have to use indentation for them to nicely format your code...

Quzah.

5. For splitting up numbers, assuming you entered them as an int to begin with, you could do:

123 % 10 = 3
123 / 10 = 12
12 % 10 = 2
12 / 10 = 1

6. >Does anybody know how to split up digits so that i can
>manipulate them?

You could convert the numer to string, there's a standard C function for it. And then you can walk through the string, get each digit and use it as a number for keeping a sum. Note that a string can be treated as an array of characters. In pseudo-code it could be something like this:

Code:
```convert_to_string (number, string)

sum = 0
index = 0

while (not end_of_string (string))
begin
value = string [index]
sum = sum + value
index = index + 1
end```
>Also wondering how to split up words??
>
>this is a sample sentence
>
>how do i break the above apart so that i can count that there
>are 5 words and however many letters there?

Use this definition: a word is a collection of digits or letters between two spaces, or between a space and end of line, or between space and start of line.

Then you can do something like this:

Code:
```start at begin of line
while not end of line
begin