What's the easiest way to count a character array as ASCII values and if it contains numeric values then convert them to the char and show them as a char array, not the values? Such input may be 1003297 the output will be d a. what is the simplest and easiest way to do that?
Actually, I am trying to solve UVa problem 444 - Encoder Decoder. I've done half of it. But The problem is I can't convert any int number input to char array.
I managed to convert the characters to their ASCII values and reverse each and every ASCII values and showing it by reversing the whole array but the vice versa I couldn't do it and stuck here for three days and keep searching. I am taking input char array so if I take numeric values, it will be taken as a character also but I need to store all the numeric values to an int array and assuming that all these numbers are ASCII values, I need to show that the char values for that int array through a char array. And one more thing that the time limit is 3 second. I know it's large but I need the easiest and simplest way to do that.
This is my incomplete solution:
#include <iostream>
#include <cstring>
#include <string.h>
#include <cstdlib>
using namespace std;
int main()
{
char str [80];
int arr [80];
char intStr[80];
int b;
string a, finale;
cout << "Your String : ";
cin.getline( str, 80, '\n' );
for(int i = 0; i < strlen(str); i++)
{
if(str[0] < 48 || str[0] > 57 || str[i] == 32 && str[i] != 13 )
{
arr[i] = str[i];
b = i;
}
else if(str[0] > 48 && str[0] < 57)
{
arr[i] = str[i];
b = i;
goto encode;
}
}
decode:
for(int j = b; j >= 0; j--)
{
itoa(arr[j], intStr, 10);
a = a + strrev(intStr);
}
cout << a;
encode:
for(int j = b; j > 0; j--)
{
//itoa(arr[j], intStr, 10);
atoi(arr[j]);
a = a + strrev(intStr);
}
cout << a;
return 0;
}
48mean the character'0'then say so. Secondly, instead of checking ranges, usestd::isdigit. Thirdly, start usingstd::stringinstead of character arrays. Fourthly, don't usegoto. Lastly,std::stoi.48,57,32or13, prefer human readable'0','9','\n'.for(int i = 0; i < strlen(str); i++): you recompute length at each iteration.std::string::size()would avoid that computation."1003297"be"d a"?