adding strings hard problem...

This is a discussion on adding strings hard problem... within the C Programming forums, part of the General Programming Boards category; how do i create a function that adds the contents of 2 strings of numbers, only n8umbers that does not ...

  1. #1
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85

    adding strings hard problem...

    how do i create a function that adds the contents of 2 strings of numbers, only n8umbers that does not use + or - or atoi function or of the like

    add_strings("123", "66")

    would return "189"

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    your saying you cant use a standard library function that would convert a string to an int? if so then your question is basically 'how do i write atoi?'. if this is the case, then try it out on paper how you would do it (ie without 'programming' or using a computer), and post your attempts or thoughts.

  3. #3
    Temporal Apparition qubit67's Avatar
    Join Date
    Jan 2007
    Posts
    85
    make a db of pre determined additions of x and y add[10][10] = ('0', '1', '2' etc)

    and than another db of carries
    Last edited by qubit67; 04-22-2007 at 12:23 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    what? if you want help please be more elaborate

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    first, answer the question from my first post: you have to do this without functions such as 'atoi' right? and your edit didnt make it much more clear or elaborate! please dont use shortforms and explain what you are saying.

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,541
    It seems like you already know the answer, and just looking for someone else to do the work.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    it sound exctly like my hw..
    but here is the answer.

    Code:
    /***************************************************************************
     *            How to add two postive numbers?
     *
     *  Sun Apr 22 09:25:07 2007
     *  Jabka Atu
     *  
     ****************************************************************************/
    
    /*
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 2 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program; if not, write to the Free Software
     *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
     */
     
     
    #include <iostream>
    using namespace std;
    
    char* add_two_postive_numbers(char *first_value,char *second_value){
    
    	char *result=NULL;
    	
    	char i=0;
    	char ans='0';
    	char carry=0;
    	char size_of_first  = strlen(first_value);
    	char size_of_second = strlen(second_value);
    	char biggest_size   = (size_of_first > size_of_second) ? size_of_first : size_of_second;
    	char smallest_size  = (size_of_first < size_of_second) ? size_of_first : size_of_second;
    	char delta = size_of_first - size_of_second;
    	
            if (size_of_first < size_of_second)
    	{
    		result=add_two_postive_numbers(second_value,first_value);
    		return result;
    	}	
    	
            result=new char[biggest_size + 2];//one more for '\0' and for extra 1
    	result[biggest_size + 1] = '\0';
    	result[0]			 = '0';
    	
    	for (i=0;i<size_of_second;i++)
    	{
    		ans=first_value[size_of_first - i-1] + second_value[size_of_second-i-1] + carry -'0';
    	        carry=0;
    									
    		if (ans > '9')
    		{
    			carry = 1;
    			result[biggest_size - i]= ans - 10;
    				
    		}
    		else 
    		{
    			result[biggest_size - i] = ans;
    		}
    	}
    	
    	if(!carry)	//since there is an option that there 
    				//will be no borrow i will cp it.
    	{
    		
    		for(i = 0;i < delta; i++)
    		{
    			result[biggest_size - size_of_second - i] = 
    				first_value[size_of_first - size_of_second - i- 1];
    		}
    	
    	}		
    	
    	if (carry)
    	{
    		for (i = 0;i < (delta)   ;i++)
    		{
    			ans=first_value[size_of_first - size_of_second - i - 1] + carry;
    			carry = 0;
    			if (ans > '9')
    			{
    				carry = 1;
    				result[biggest_size-size_of_second - i] = ans - 10 ;
    			}
    			else 
    			{
    				result[biggest_size - size_of_second - i]	= ans ;
    			}	
    			
    		}//end of for
    		if (carry)
    		{
    			result[0]='1';
    		}		
    	}//end of if (exsitence of an carry
    	
    	return result;
    }
    Last edited by jabka; 04-22-2007 at 12:32 AM. Reason: spelling errors ...

  8. #8
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    the first thing you need is to create a function that returns the integer value of a char:
    Code:
    int char2int(char c);
    once you have this we can work out the next step, which would be to figure out how to convert between your two 'languages' (ie string and integers).
    some examples (note that you would already have a way to convert a char to an int, so everything is represented using ints):

    1 = 10^0 * 1
    2 = 10 ^0 * 2
    10 = 10^1 * 1
    15 = 10^1 * 1 + 10^0 *5
    Last edited by nadroj; 04-22-2007 at 12:54 AM.

  9. #9
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    @jabka Please don't post stuff like that, He has to do the work, it's his homework.

    Also you've posted C++ code in the C section...

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by nadroj View Post
    the first thing you need is to create a function that returns the integer value of a char
    Characters already have an integer value. They're also nicely sequenced, so all you have to do is subtract the value of the character zero from one to see if it's zero through nine. I can see if you just enjoy over complicating things, but it's hardly needed here.


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

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i know they have a character value, and im not trying to overcomplicate things. what if the character is 'z' this would screw up though, right?

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    signed int x = 'z' - '0';
    if( x < 0 || x > 9 )
        puts( "You didn't enter a digit." );
    That's what error checking is for. You could always just use isdigit.


    Quzah.
    Last edited by quzah; 04-22-2007 at 01:16 AM.
    Hope is the first step on the road to disappointment.

  13. #13
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im assuming he cant use functions like that, but i was still waiting for the answer from my previous post.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You don't need a function for it. Just basic subtraction, as I mentioned. Actually, you don't even need subtraction for digit testing. All you have to do is compare it to the range of zero to nine as a character.


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

  15. #15
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    ok sounds fair. either way would work. i just never thought about the ascii characters.

    edit:
    All you have to do is compare it to the range of zero to nine as a character.
    something like this?:
    Code:
    if (myChar == '1')
       myDigit = 1;
    //etc
    if thats the case, then thats exactly what i was thinking of to make a function. or you could just do it in the loop that hell need in the final product, but id just like to seperate this.
    Last edited by nadroj; 04-22-2007 at 01:25 AM.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 12:01 PM
  2. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 12:07 PM
  3. Problem with else on strings
    By [Z-D] in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 09:41 PM
  4. array of strings problem
    By dayknight in forum C Programming
    Replies: 3
    Last Post: 11-08-2005, 09:41 AM
  5. Replies: 5
    Last Post: 05-25-2004, 04:36 PM

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