Thread: Adding numbers in string

  1. #1
    Registered User
    Join Date
    Aug 2016
    Posts
    4

    Adding numbers in string

    This is my first post..i have an issue to write code for this problem.

    Add numbers in string:
    testcase:
    string:

    Input

    2
    abc100asd100
    20cc33

    Output

    200
    53

    Explanation

    100+100=200
    20+33=53

    I can write program to add all the numbers(1+0+0+1+0+0) in this string
    but i find very difficult add (100 + 100)
    please help me.please give me a suitable program for this.
    Thanks in advance

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You could utilise the fact that sscanf with the "%d" format will stop conversion when it finds a character outside the range '0' to '9'.

    You could also look at say isdigit() character test in ctype.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2016
    Posts
    2
    hey I am new here and I dont realice a question

  4. #4
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Quote Originally Posted by ccgt View Post
    hey I am new here and I dont realice a question
    What do you mean? I don't understand the question.
    Double Helix STL

  5. #5
    Registered User
    Join Date
    Aug 2016
    Posts
    4
    can anyone guide me please.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by shunmuga View Post
    can anyone guide me please.
    You've had a week - do you have any code to show us?

    You said you had some code, post it so we can see where the real issues are.

    We're not just going to dump working code for you to hand in without fully understanding it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    Sep 2016
    Posts
    7
    Code:
    #include <stdio.h>
    
    int string_to_num();
    
    int main() {
        int length = 0;
        int buff = 0;
    
        printf("Enter Strings:");
    
             while((length = string_to_num()) > 0) {
            buff = buff+length;
            printf("\n %d \n",length);
        }
        return 0;
    }
    
    int string_to_num() {
        int c;
        int oldval = 0;;
        int buff = 0;
        int first_num = -1;
        for(c = 0; c = getchar(), c != EOF && c != '\n';) {
            if(c >= '0' && c <= '9') {
                     buff = (oldval * 10) + (c-48);
                oldval = buff;
            }
            else if(first_num == -1 && oldval != 0) {
                first_num = oldval;
                buff = oldval = 0;
        
            }
        }
            return buff + first_num;
    }

  8. #8
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688
    Please do not provide solutions to problems. Give tips and advice,
    Double Helix STL

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding numbers that user enters as a single string
    By metalinspired in forum C++ Programming
    Replies: 10
    Last Post: 04-28-2006, 10:18 AM
  2. Numbers adding together = bad
    By Zyk0tiK in forum C Programming
    Replies: 5
    Last Post: 12-04-2005, 04:37 PM
  3. Adding big numbers
    By FoodDude in forum C++ Programming
    Replies: 4
    Last Post: 11-18-2005, 03:36 PM
  4. adding numbers
    By Allen_Buchannon in forum C++ Programming
    Replies: 1
    Last Post: 03-29-2004, 05:43 PM
  5. adding odd numbers
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 09-06-2001, 01:44 PM

Tags for this Thread