简体   繁体   中英

C++ How to output the letters or numbers from input of letters or numbers

So let's say we have the following case: for ”12323465723” possible answers would be ”abcbcdfegbc” (1 2 3 2 3 4 6 5 7 2 3), ”awwdfegw” (1 23 23 4 6 5 7 23), ”lcwdefgw” (12 3 23 4 6 5 7 23), in this case, the user will input numbers from 1 to 26, not divided by any space and the program itself will suggest 3 ways of interpreting the numbers, getting the most of the combinations from 1 to 26 these being the values from a to z

As you can see this is edited, as this is the last part of the problem, Thank you all who have helped me this far, I've managed to solve half of my problem, only the above mentioned one is left.

SOLVED -> Thank you

This involves a decision between 0 to 2 outcomes at each step. The base cases are there are no more characters or none of them can be used. In the latter case, we backtrack to output the entire tree. We store the word in memory like dynamic programming . This naturally leads to a recursive algorithm.

#include <stdlib.h> /* EXIT */
#include <stdio.h> /* (f)printf */
#include <errno.h> /* errno */
#include <string.h> /* strlen */

static char word[2000];
static size_t count;

static void recurse(const char *const str) {

    /* Base case when it hits the end of the string. */
    if(*str == '\0') { printf("%.*s\n", (int)count, word); return; }

    /* Bad input. */
    if(*str < '0' || *str > '9') { errno = ERANGE; return; }

    /* Zero is not a valid start; backtrack without output. */
    if(*str == '0') return;

    /* Recurse with one digit. */
    word[count++] = *str - '0' + 'a' - 1;
    recurse(str + 1);
    count--;

    /* Maybe recurse with two digits. */
    if((*str != '1' && *str != '2')
        || (*str == '1' && (str[1] < '0' || str[1] > '9'))
        || (*str == '2' && (str[1] < '0' || str[1] > '6'))) return;
    word[count++] = (str[0] - '0') * 10 + str[1] - '0' + 'a' - 1;
    recurse(str + 2);
    count--;
}

int main(int argc, char **argv) {
    if(argc != 2)
        return fprintf(stderr, "Usage: a.out <number>\n"), EXIT_FAILURE;
    if(strlen(argv[1]) > sizeof word)
        return fprintf(stderr, "Too long.\n"), EXIT_FAILURE;
    recurse(argv[1]);
    return errno ? (perror("numbers"), EXIT_FAILURE) : EXIT_SUCCESS;
}

When run on your original input, ./a.out 12323465723 , it gives,

abcbcdfegbc
abcbcdfegw
abcwdfegbc
abcwdfegw
awbcdfegbc
awbcdfegw
awwdfegbc
awwdfegw
lcbcdfegbc
lcbcdfegw
lcwdfegbc
lcwdfegw

(I think you have made a transposition in lcwdefgw .)

If you want to count the occurencs of "ab" then this will do it:

int main()
{
    char line[150];
    int grup = 0;

    cout << "Enter a line of string: ";
    cin.getline(line, 150);
    for (int i = 0; line[i] != '\0'; ++i)
    {
        if (line[i] == 'a' && line[i+1] == 'b')
        {
            ++grup;
        }
    }

    cout << "Occurences of ab: " << grup << endl;

    return 0;
}

If you want to convert an int to an ASCII-value you can do that using this code:

// Output ASCII-values
int nr;
do {
cout << "\nEnter a number: ";
cin >> nr;
nr += 96; // + 96 because the ASCII-values of lower case letters start after 96
cout << (char) nr;
} while (nr > 96 && nr < 123);

Here I use the C style of casting values to keep things simple. Also bear in mind ASCII-values: ASCII Table

Hope this helps.

According to ASCII table we know that from 65 to 90 it A to Z. so below is the simple logic to achieve what you're trying.

int main(){
int n;
cin>>n;
n=n+64;
char a=(char) n;
if (a>=64 && a<=90)
    cout<<a;
else cout<<"Error";
return 0;
}

This could be an interesting problem and you probably tagged it wrong, There's nothing specific to C++ here, but more on algorithm.

First of all the "decode" method that you described from numerical to alphabetical strings is ambiguious. Eg., 135 could be interpreted as either "ace" or "me". Is this simply an oversight or the intended question?

Suppose the ambiguity is just an oversight, and the user will enter numbers properly separated by say a white space (eg., either "1 3 5" or "13 5"). Let nstr be the numerical string, astr be the alphabetical string to count, then you would

  1. Set i=0 , cnt=0 .
  2. Read the next integer k from nstr (like in this answer ).
  3. Decode k into character ch
    1. If ch == astr[i] , increment i
    2. If i == astr.length() , set i=0 and increment cnt
  4. Repeat from 2 until reaching the end of nstr .

On the other hand, suppose the ambiguous decode is intended (the numerical string is supposed to have multiple ways to be decoded), further clarification is needed in order to write a solution. For example, how many k 's are there in "1111" ? Is it 1 or 2, given "1111" can be decoded either as aka or kk , or maybe even 3, if the counting of k doesn't care about how the entire "1111" is decoded?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM