473,324 Members | 2,313 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

how to subtract two string containing numbers?

6
hi.i have a code for subtract of two char arrays(size of each is 50).but i want to write this program with string and i should be consider that size of each string may be not equals.for example we have s1=4777 and s2=55.and we should be subtract them.this is my code.
Expand|Select|Wrap|Line Numbers
  1. char* sub(char x[50],char y[50])
  2. {
  3. int i,j,temp=0;
  4. char z[50]={'0'};
  5. for(i=0;i<50;i++)
  6. {
  7. z[i]='0';
  8. }
  9. for(i=49;i>=0;i--)
  10. {
  11. temp=x[i]-y[i];
  12. if(temp>=0)
  13. {
  14. z[i]=temp+48;
  15. }
  16. else
  17. {
  18. j=i-1;
  19. while(x[j]=='0')
  20. {
  21. x[j]='9';
  22. j--;
  23. }
  24. x[j]=x[j]-1;
  25. z[i]=((x[i]-48)-(y[i]-48)+10)+48;
  26. }
  27. }
  28. for(i=0;i<50;i++)
  29. {
  30. x[i]=z[i];
  31. }
  32. return z;
  33.  
Mar 29 '14 #1
1 6074
weaknessforcats
9,208 Expert Mod 8TB
I would convert each string to an int and then subtract the ints.

You can use strtol to do the conversion for you. Something like:

strtol(x) - strtol(y)

will convert each string to a temporary int and subtract the ints.
Mar 30 '14 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Chris Michael | last post by:
I've got a string of numbers, say 123456 (the actually number is 12 digits long). I need to calculate the sum of each individual number in the string of numbers, so in the example of 123456 the sum...
2
by: Remi Caron | last post by:
Hi, I took over an Visual Object project (Visual Clipper) in that language there is a function to: Convert a string containing a 32-bit binary date to a date data type. That function is called...
7
by: Li Pang | last post by:
Hi, I'd like to know how to call a sub/function bu using a string containing its name, as an example as follow: Sub Main() Dim name As String = "TestMe()" ' need help here End Sub
4
by: Don | last post by:
Is it possible to access the property of an object using a string containing the name of the property you want to access? Something like: oCustomer = New Customer propname = "FirstName"...
3
by: Tran Hong Quang | last post by:
Hi, I am new to C. How to generate an fixed-length string containing an random digits? for example string of 5 characters, the value can be 03234 or 23423 or 02343 Thanks Tran Hong Quang
7
by: Eric Lilja | last post by:
Hello, I have an unsigned long that I need to convert to a std::string. The unsigned long holds 32-bit checksums and sometimes the most significant byte is 0 and in those cases the string should be...
5
by: archana | last post by:
Hi all, I want to apply regular expresion on string containing chatacters like '(,')',+' and many such which we are using while creating pattern for regulat expression. But when i am applying...
7
by: bob | last post by:
Hi, I need to make a string containing this text litterally as it:: selectcommand="select * from table where field1=' " & myvar & " ' " Dim st as string st="selectcommand=" & ??? How can i...
2
by: pankajit09 | last post by:
Hello, I have a textbox in which I have to enter a string of numbers. The numbers are to be converted in to a Binary Search Tree and then I have to determine whether the BST is a --> 1....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.