Please help with strings

Hi

I am new to c++, I have found some tasks to do, but there is a problem. My task is to compare 2 intigers, and try to find out if they an be changed so one of them is bigger than other, for example:

input: 35 and 45 can be changed to 53 and 45.

I am trying to get 2 numbers and to put them in arrays to sort them, but I dont know how to do char to array and back, can you give me an example or link to the code please so I can change the values in array (for example if I get 10 then I want to change 0 to something)? All the comparing I know how to do.

Thank you.
Last edited on
If you just have two numbers, you don't need arrays or anything.
if (n1>n2)swap(n1,n2); assures that n1 is not larger than n2.

I dont know how to do char to array and back

Not sure what you mean.

If you want to extract digits from the numbers: % gives you the remainder of a division, which lets you do this easily.
I need to sort the digits in the char descending.

I changed my code, and now the problem is:

I read user input:
1
2
char arr1[51];
cin >> arr1;


(the same for other one)

I do the sorting etc., but how can I compare these two arrays?

I mean if one array holds: 3 | 2 | 1
The other: 4 | 1 | 0

How do I know whic number is bigger 321 or 410?
Last edited on
Ah, I assumed the numbers always were supposed to have two digits.
You shouldn't use char arrays when not necessary, use std::string instead.
You can use std::sort to sort your digits as follows:

1
2
3
string n1;
cin >> n1;
sort(n1.begin(),n1.end(),greater<char>());


sort could also be used on regular arrays.
Use

#include <string>

so:

1
2
3
4
std::string input;
std::cin >> input;

int value = atoi(input.c_str());
Topic archived. No new replies allowed.