Splitting Digits and Letters

This is a discussion on Splitting Digits and Letters within the C Programming forums, part of the General Programming Boards category; Does anybody know how to split up digits so that i can manipulate them? eg.. for example the user types ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    10

    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. #2
    noho
    Guest
    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. #3
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    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;
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Ya know, code tags are nice, but you actually have to use indentation for them to nicely format your code...

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    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. #6
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    >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
        read letters until space found
        if there are more spaces then read them until letter found
    end
    During reading you could count the number of letters you encounter. Each time you start reading a new word, increase a counter which counts the words.

  7. #7
    Registered User
    Join Date
    Oct 2002
    Posts
    10
    Thanks for the help all but i'm still a little confused.

    - Stoned_Coder: your code doesnt work properly

    - Shiro: what is the standard C function to convert numbers to strings? Also, can I use the ASCII character codes to help me with this?
    Last edited by wordup; 10-08-2002 at 03:17 PM.

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    oops my apologies for bugged code and bad indentation. was just typing in browser and never checked that it compiled correctly.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 12:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21