-4

I want to take input from user in

char input[200];

Then I want to swap each character of that input according to

a='y'; b='b'; c='j'; d='m'; e='x'; f='f'; g='w'; h='i';
i='v'; j='c'; k='l'; l='u'; m='t'; n='a'; o='k'; p='h';
q='d'; r='p'; s='s'; t='n'; u='z'; v='q'; w='e'; x='r';
y='o'; z='g';

For example if the input is

hello

The output will be

ixuuk

I want to code in C++ using for loops and arrays.

4 Answers4

1

I suggest you use a lookup array:

char output = conversion[input_char];

You can simplify the array to 26 letters by using some arithmetic:

char output = conversion[input_char - 'a'];

The expression input_char - 'a' makes the letter a refer to the first slot in the conversion array.

Here's an example of the array:

static const char conversion[] =
{'b', 'y', 'c', 'k', 'f', /*...*/, 'a'};

Using the above code, if input is a, the output will be b. For input of b, the output will be y, and so on.

No need to swap. Remember that swapping changes values. I believe you want conversion or translation instead.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

Here is a demonstrative program that shows how it could be done

#include <iostream>
#include <cstring>
#include <utility>

int main() 
{
    std::pair<const char *, const char *> cipher =
    {
        "abcdefghijklmnopqrstuvwxyz",   
        "ybjmxfwivclutakhdpsnzqerog"    
    };

    const size_t N = 200;
    char input[N];
    input[0] = '\0';

    std::cin.getline( input, sizeof( input ) );

    std::cout << '\"' << input << '\"' << std::endl;

    for ( char *p = input; *p; ++p )
    {
        if ( const char *q = std::strchr( cipher.first, *p ) )
        {
            *p = cipher.second[q - cipher.first];
        }
    }

    std::cout << '\"' << input << '\"' << std::endl;
    return 0;
}

The program output is

"Hello"
"Hxuuk"

You could also use functions tolower or toupper to convert initially characters of the source string to some case.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • http://prntscr.com/7arano I got the idea but stuck on a line of code. Please see the screenshot from above link and response. Regards – Asad Jamal Malik May 29 '15 at 14:17
  • @Asad Jamal Malik I am sorry but I do not look through screen shots. – Vlad from Moscow May 29 '15 at 14:21
  • Okay well, it is saying [Error] in C++ 98 'cipher' must be initialized by constructor, not by '{...}' – Asad Jamal Malik May 29 '15 at 16:10
  • @Asad Jamal Malik It might be that you have an old compiler. In the definition of cipher replace braces with parentheses like this std::pair cipher ( "abcdefghijklmnopqrstuvwxyz", "ybjmxfwivclutakhdpsnzqerog" ); Notice that the assignment operator is removed. – Vlad from Moscow May 29 '15 at 16:22
0

Solved the problem

#include <iostream>
#include <cstring>
#include <utility>

int main() 
{
    std::pair<const char *, const char *> 

cipher ("abcdefghijklmnopqrstuvwxyz",   
        "ybjmxfwivclutakhdpsnzqerog");



    const size_t N = 200;
    char input[N];
    input[0] = '\0';

    std::cin.getline( input, sizeof( input ) );

    std::cout << '\"' << input << '\"' << std::endl;

    for ( char *p = input; *p; ++p )
    {
        if ( const char *q = std::strchr( cipher.first, *p ) )
        {
            *p = cipher.second[q - cipher.first];
        }
    }

    std::cout << '\"' << input << '\"' << std::endl;
    return 0;
}

Special thanks to @Vlad from Moscow

0

i suggest you use the nested loop. create an array of strings having all the alphabets. then create another array of strings having all the characters you want to change with the alphabets in the same order of the alphabets.

    #include <iostream>
    using namespace std;
    int main()
    {
        string alphabets="abcdefghijklmnopqrstuvwxyz" , 
        cipher="ybjmxfwivclutakhdpsnzqerog" , word , newword="" ;
        cin>>word;
        newword=word;
        for(int i=0;i<26;i++)
        {
            for(int j=0;j<26;j++)
            {
                if(word[i]==alphabets[j])
                {
                    newword[i]=cipher[j];
                    break;
                }
            }
        }
        cout<<newword;
        return 0;
    }