need quick help guys!

I have to encode "Hello World".The way to do this is by using the last 4 numbers of my roll number. Example scenario:
If roll no is 18F-0805. So Code 1 is 0+8+1 = 9 and Code 2 is 0+5+1 = 6.
Now For Capital letters Code is 9 and For Small letters, Code is 6.
Input: Hello World! H will turn to Q (an increase of 9) but small letters will increment by 6
Output: Qkrru Fuxrj
Now the only problem is that how will I deal with alphabets like 'r' which will cross 'z' due to shifting.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
#include<string>
using namespace std;
void GenerateCode(int* p1,int* p2,char* line);
void Decode(int* p1,int* p2,char* line);
int main()
{
	int a=0+2+1,b= 9+6+1;
	int *P1; 
	int *P2;
	P1=&a;   
	P2=&b;
	char line[] = "Hello World";
    GenerateCode(P1,P2,line);
}
void GenerateCode(int* P1,int* P2,char* line)
{
	int i;
     for(int i = 0; i <10; i++)
     {
     	if (line[i] == 'H' || line[i] == 'W')
     	{
     		line[i]= line[i]+*P1;
		}
	    if (line[i]== 'e'|| 'l' || 'o' || 'r' || 'd' )
	    {
	    	line[i]=line[i]+*P2;
		}
	cout<<line[i];
	}
	 		
}
> Now the only problem is that how will I deal with alphabets like 'r' which will cross 'z' due to shifting.

Make it roll over to the beginning of the alphabet: 'z' + 1 => 'a', 'Z' + 2 => 'B' etc

Something like this
(note that this code contains repetitive patterns, which can be factored into small functions)
(also note that it does not blindly assume that 'A' + 1 would yield 'B' etc.):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <iostream>
#include <string>

const std::string uppercase_letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
const std::string lowercase_letters = "abcdefghijklmnopqrstuvwxyz" ;

std::string encode( const std::string& str, std::size_t offset_upper_case, std::size_t offset_lower_case )
{
    std::string result ;
    for( char c : str ) // for each character in the input string
    {
        // check if it is an upper case letter
        auto pos = uppercase_letters.find(c) ;
        if( pos != std::string::npos ) // upper case letter, found at position pos
        {
            pos += offset_upper_case ; // add offset for upper case letters
            pos %= uppercase_letters.size() ; // adjust pos if it is beyond end of string
            result += uppercase_letters[pos] ; // add encoded character to result
        }

        // otherwise check if it is a lower case letter
        else if( ( pos = lowercase_letters.find(c)) != std::string::npos ) // lower case letter, found at position pos
        {
            pos += offset_lower_case ; // add offset for lower case letters
            pos %= lowercase_letters.size() ; // adjust pos if it is beyond end of string
            result += lowercase_letters[pos] ; // add encoded character to result
        }

        // not an alpha character, add it as it is to the result
        else result += c ;
    }

    return result ;
}

std::string decode( const std::string& str, std::size_t offset_upper_case, std::size_t offset_lower_case )
{
    std::string result ;
    for( char c : str ) // for each character in the input string
    {
        // check if it is an upper case letter
        auto pos = uppercase_letters.find(c) ;
        if( pos != std::string::npos ) // upper case letter, found at position pos
        {
            pos += uppercase_letters.size() ; // to make sure it does not become less than zero
            pos -= offset_upper_case ; // subtract offset for upper case letters
            pos %= uppercase_letters.size() ; // adjust pos if it is beyond end of string
            result += uppercase_letters[pos] ; // add encoded character to result
        }

        // otherwise check if it is a lower case letter
        else if( ( pos = lowercase_letters.find(c)) != std::string::npos ) // lower case letter, found at position pos
        {
            pos += uppercase_letters.size() ; // to make sure it does not become less than zero
            pos -= offset_lower_case ; // subtract offset for lower case letters
            pos %= lowercase_letters.size() ; // adjust pos if it is beyond end of string
            result += lowercase_letters[pos] ; // add encoded character to result
        }

        // not an alpha character, add it as it is to the result
        else result += c ;
    }

    return result ;
}

int main()
{
    const std::size_t offset_ucase = 9 ;
    const std::size_t offset_lcase = 6 ;

    // note: 123 etc. would remain unchanged during the encode/decode process
    //       xyz would overflow 'z' when offset is added
    const std::string str = "Hello 123 World xyz!" ;
    std::cout << "original: " << str << '\n' ;

    const std::string encoded_str = encode( str, offset_ucase, offset_lcase ) ;
    std::cout << " encoded: " << encoded_str << '\n' ;

    const std::string decoded_str = decode( encoded_str, offset_ucase, offset_lcase ) ;
    std::cout << " decoded: " << decoded_str << '\n' ;
}

http://coliru.stacked-crooked.com/a/bfa9a9f4dcbb923e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>
using namespace std;

const string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";


string change( string text, string in, string out )
{
   for ( char &c : text )
   {
      int pos = in.find( c );
      if ( pos != string::npos ) c = out[pos];
   }
   return text;
}


class Code
{
   string BETALPHA;
public:
   Code( int uc, int lc )
   {  
      BETALPHA = ALPHABET.substr(      uc, 26 - uc ) + ALPHABET.substr(  0, uc )
               + ALPHABET.substr( 26 + lc, 26 - lc ) + ALPHABET.substr( 26, lc );
   }
   string encode( string text ) { return change( text, ALPHABET, BETALPHA ); }
   string decode( string text ) { return change( text, BETALPHA, ALPHABET ); }
};


int main()
{
   Code code( 9, 6 );
   string text = "Hello World!";   cout << text << '\n';
   text = code.encode( text );     cout << text << '\n';
   text = code.decode( text );     cout << text << '\n';
}


Hello World!
Qkrru Fuxrj!
Hello World!
Last edited on
Topic archived. No new replies allowed.