0

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;
}
8
  • 3
    First of all stop using magic numbers. If you by e.g. 48 mean the character '0' then say so. Secondly, instead of checking ranges, use std::isdigit. Thirdly, start using std::string instead of character arrays. Fourthly, don't use goto. Lastly, std::stoi. Commented Jul 31, 2018 at 13:59
  • Avoid magic numbers as 48, 57, 32 or 13, prefer human readable '0', '9', '\n'. Commented Jul 31, 2018 at 13:59
  • for(int i = 0; i < strlen(str); i++): you recompute length at each iteration. std::string::size() would avoid that computation. Commented Jul 31, 2018 at 14:03
  • Should not "1003297" be "d a"? Commented Jul 31, 2018 at 14:20
  • Yes if input is 1003297 the output should "d a" .if i use in condition if like if(str[0]<'0' || str[o]>'9' && str[i]!= '\n') will it Work Fine? I have Corrected For Loop now. And Now What is The Way or simplest and easiest way to Count numeric number as Ascii values and output the ascii values as Char array? Commented Jul 31, 2018 at 14:40

1 Answer 1

2

It can certainly be made simpler...

First, usage of raw character arrays in C++ should be reserved to low level operations. Here you are just processing an input string, so it should be a plain std::string.

Then the correct way is to accumulate input decimal digits until you reach an acceptable ascii code, and when you get one you add it at the end of your output string.

Finally, building a string one character at a time is inefficient: you should first gather everything in a std::vector and only at the end build the string. Code could be as simple as:

#include <iostream>
#include <string>
#include <vector>

using namespace std;
int main()
{
    string line;            // input line
    vector<char> out;       // gather converted characters
    char res = 0;           // to compute the ascii values
    std::getline(cin, line);
    for (char c: line) {
        if (c >= '0' && c <= '9') {         // only process decimal digits
            res = 10 * res + (c - '0');
            if (res >= ' ') {               // Ok, we have a valid ASCII code
                out.push_back(res);
                res = 0;
            }
        }
        else if (res != 0) {                // assume that non numeric act as separators
            out.push_back(res);             // whatever the input value (allows control chars)
            res = 0;
        }
    }
    string a(out.begin(), out.end());       // build a string from the vector
    cout << a << endl;
    return 0;

}
Sign up to request clarification or add additional context in comments.

6 Comments

Trying to Run These Whole Code But Only Black Screen Appears And Didnt Show Any Output For Any Input By this Code?!
I gave it 1003297Enter and got d a.
Nothing Happens I am Trying It on codeblocks 17.12. After Build It Says a Note In the log File "C:\Users\Prince Porosh\Documents\f.cpp|12|warning: range-based 'for' loops only available with -std=c++11 or -std=gnu++11|"
Does This Kind Of For Not Supported by Codeblocks??Or IN Which IDE you are Running Plz Let me Know.
@user9903364: no IDE for that, just vim to edit the code and Clang to compile it in C++11 mode.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.