13 Replies - 1697 Views - Last Post: 27 March 2011 - 07:42 AM Rate Topic: -----

#1 faranrasheed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-March 11

how to add two strings ?

Posted 27 March 2011 - 06:25 AM

lets say i have a string S[10] ,in which i have large integer say 12345678 and another string of same size having large integer but less than the first string say 12345 or greater than the first string say 1234567891 .Now i want to add these two strings.
for example :
if string1=123456789
and string2=12345
then the answer should be 123469134

Is This A Good Question/Topic? 0
  • +

Replies To: how to add two strings ?

#2 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: how to add two strings ?

Posted 27 March 2011 - 06:28 AM

why do you not just use integer values ?
Was This Post Helpful? 0
  • +
  • -

#3 sakshamkum  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • View blog
  • Posts: 232
  • Joined: 09-June 09

Re: how to add two strings ?

Posted 27 March 2011 - 06:34 AM

first use arrays of equal size and store the numbers in right alignment and keep a variable carry and keep adding digits form last to first and with the carry

first use arrays of equal size and store the numbers in right alignment and keep a variable carry and keep adding digits form last to first and with the carry

first use arrays of equal size and store the numbers in right alignment and keep a variable carry and keep adding digits form last to first and with the carry
Was This Post Helpful? 1
  • +
  • -

#4 faranrasheed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-March 11

Re: how to add two strings ?

Posted 27 March 2011 - 06:42 AM

i am using strings because i have to read these two integers from a file and they are too long that can't be handle by int or long int so i am using string.

@sakshamkum:
i am using arrays of same size but i am not having any idea to add two integers .
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: how to add two strings ?

Posted 27 March 2011 - 06:58 AM

sakshamkum's solution is more versatile -- it can handle strings of any length.

sk1v3r's idea is easier, but it will work only if the strings are short enough so that the resulting int values don't exceed INT_MAX on your particular system.

On a 32 bit system that would limit you to 9 digits. Depending on your compiler you might be able to use long long int which I believe would let you add up to 18 digit integers safely.

On a 64 bit system the limits would be correspondingly higher. On an old 16 bit system you would be limited to only 4 digits.
Was This Post Helpful? 1
  • +
  • -

#6 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: how to add two strings ?

Posted 27 March 2011 - 07:01 AM

first off, know the size of each of the strings, and you should have them set out something like this
position : 1 2 3 4 5 6 7 8
string 1 : 1 2 3 4 5 6 7 8 // so this one is just 12345678
string 2 : 0 0 0 0 1 2 3 4 // so this one is just 1234
and then, from the right, add up the numbers, and if it is more than ten, take away ten and add ten to the value on the left.
Hope This Helps :)
(this is just an example of sakshamkum's idea)

This post has been edited by sk1v3r: 27 March 2011 - 07:03 AM

Was This Post Helpful? 1
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: how to add two strings ?

Posted 27 March 2011 - 07:10 AM

View Postfaranrasheed, on 27 March 2011 - 08:42 AM, said:

i am using strings because i have to read these two integers from a file and they are too long that can't be handle by int or long int so i am using string.

@sakshamkum:
i am using arrays of same size but i am not having any idea to add two integers .


Use the ASCII value of the digits. Look at an ASCII chart (google). The character '0' has an ASCII value of 48. '1' has the value 49. And so on...

So you can easily convert a char digit to its actual numerical value simply by subtracting 48. For example:
int v = '5' - 48;
will give v the value 5.
Was This Post Helpful? 1
  • +
  • -

#8 faranrasheed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-March 11

Re: how to add two strings ?

Posted 27 March 2011 - 07:12 AM

r.stiltskin idea is simple but i prefer you must read question .here it is :

"The current 32 and 64 bit machines can represent integers of around 9 digits and 30 digits
respectively, while various scientific and mathematical applications have to represent
integers as large as 200 to 300 digits. Implement a class that can store and manipulate
arbitrarily large signed integers of length up to 200 digits."
Was This Post Helpful? 0
  • +
  • -

#9 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: how to add two strings ?

Posted 27 March 2011 - 07:19 AM

And if you look at this thread, that question has been answered :)
except that, the question tells you to create your own class, not use strings ... so you might have got the wrong idea ...

This post has been edited by sk1v3r: 27 March 2011 - 07:20 AM

Was This Post Helpful? 0
  • +
  • -

#10 faranrasheed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-March 11

Re: how to add two strings ?

Posted 27 March 2011 - 07:26 AM

View Postr.stiltskin, on 27 March 2011 - 07:10 AM, said:

View Postfaranrasheed, on 27 March 2011 - 08:42 AM, said:

i am using strings because i have to read these two integers from a file and they are too long that can't be handle by int or long int so i am using string.

@sakshamkum:
i am using arrays of same size but i am not having any idea to add two integers .


Use the ASCII value of the digits. Look at an ASCII chart (google). The character '0' has an ASCII value of 48. '1' has the value 49. And so on...

So you can easily convert a char digit to its actual numerical value simply by subtracting 48. For example:
int v = '5' - 48;
will give v the value 5.


how this will give v the value 5 ?
could you please explain it to me..?
Was This Post Helpful? 0
  • +
  • -

#11 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: how to add two strings ?

Posted 27 March 2011 - 07:32 AM

because the character '0' has the value of 48.
char zero = '0';
int zero_int = zero;
cout << zero_int; // this prints out 48


'1' has the value of 49, '2' has the value of 50 ... '9' has the value of 57

so if you take away 48, you get the number, instead of the character.
char test = '0';
int inttest = test - 48;
cout << test << " : " << inttest; // prints out 0 : 0



Was This Post Helpful? 1
  • +
  • -

#12 faranrasheed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-March 11

Re: how to add two strings ?

Posted 27 March 2011 - 07:36 AM

View Postsk1v3r, on 27 March 2011 - 07:32 AM, said:

because the character '0' has the value of 48.
char zero = '0';
int zero_int = zero;
cout << zero_int; // this prints out 48


'1' has the value of 49, '2' has the value of 50 ... '9' has the value of 57

so if you take away 48, you get the number, instead of the character.
char test = '0';
int inttest = test - 48;
cout << test << " : " << inttest; // prints out 0 : 0




Thanks i got the idea , and now i have converted whole string into array of integers but there is only one mistake that is the size of string is 200 and if i am having 10 digits inside it than the array of integers have these ten digits but after that it includes -48 because there is 0 present and 0-48 is -48.So how to get rid of that.?
Was This Post Helpful? 0
  • +
  • -

#13 sk1v3r  Icon User is offline

  • D.I.C Addict

Reputation: 231
  • View blog
  • Posts: 668
  • Joined: 06-December 10

Re: how to add two strings ?

Posted 27 March 2011 - 07:41 AM

if it is less than zero, then make it zero. Simple I guess :)
if(number < 0)
{
    number = 0;
}


Was This Post Helpful? 0
  • +
  • -

#14 faranrasheed  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 24-March 11

Re: how to add two strings ?

Posted 27 March 2011 - 07:42 AM

Oh God!
Its done thanks to all....
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1