Need help in encoding a string

Hello guys! I am stuck in this program. Its giving errors. Basically, we 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!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
  #include<iostream>
using namespace std;
void GenerateCode(int* q,int* w,char* c);
int main()
{
	char x[] = "18f-0296";
	int a =  x[4] + x[5]+ 1; // adding 0+2 +(1) of the roll number
	int b= x[6] + x[7] +1; // adding 9+6+(1) of the roll number 
	int *P1; 
	int *P2;
	P1=&a;   
	P2=&b;
	char line = "Hello World";
	GenerateCode(int &P1 ,int &P2,char &line);
}
void GenerateCode(int* q,int* w,char* c)
{
	
	cout << c[0]+q << c[1]+w << c[2]+w <<c[3]+w <<c[4]+w << " " << c[5]+q << c[6]+w <<c[7]+w << c[8]+w <<  c[9]+w; // encoding
		
}
Last edited on
you are trying to assign a string to a character. a character holds 1 byte.
line 13... should be string line = "Hello World". For that you need <string> included.
you also need to make generate code take a string, of course, to match.
stringvariable[x] works just as you have it, once you do that.

pure addition won't work, by the way.
'W'+9 is not 'F', its going to be some other ascii value.
you want to force it to wrap back around, which needs a little more math.
there are cute ways to do it but for now a simple if letter+9 > 'Z' then ... will do it.

if you want to get really shifty you can do this
char uptbl[26];
for(i = 0; i < 26; i++)
uptbl[i] = ('A' + i +9)%26;
and just do a lookup to get your values. lookup is uptbl[upperlettervalue-'A']
so 'A'-'A' is zero, uptbl[0] is 'A'+9 ... right? and similar for lower case... just don't hard code the 9, its here to show you the way, make it your variable in the code.
Last edited on
these are the instruction!
ROLL NO: 18F-WXYZ
Now write a Program in which you declare 2 integers a=W+X+1 and b=Y+Z+1. Now create 2
Pointers, P1 will point to ‘a’ and P2 will point to ‘b’. Then write a function void
GenerateCode(int* P1,int* P2,char* line). This function takes a line by reference and changes it into the encrypted line.
my function declaration will be as mentioned!
Is there any problem in line 19?
I changed the code. But again the output is wrong. It's printing some weird characters.


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];
	}
	 		
}
if (line[i]== 'e'|| 'l' || 'o' || 'r' || 'd' )

compilers cannot read human intentions.
this says
if(line[i] is e or true or true or true or true)
you need to put each compare explicitly
if(line[i] == 'e' || line[i]=='o' || etc...)

x +=3 is the same as x = x+3. use the shortcut operators when you can its easier to read.

I am not convinced your new approach is correct either, see what you get with the conditional bug fixed.
Last edited on
Now the only problem is that how will I deal with alphabets like 'r' which will cross 'z' due to shifting.
The letters which end up before z are correctly encoded but the letters which cross z are again turning to weird characters..
I said that :)
you need to wrap back around. if letter + value > z then ... something is the most simple to understand fix.

so what goes in there..
if its > z, then you have to get to z (say its y, then you add 1, and you still have 8 if you were adding 9, so now its a+8) so you need to know the amount left over to wrap around...
Last edited on
Start with a function that rotates the characters. Here's an example:
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
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

// Given a character, rotate it "upperCount" characters if it's
// upper case and "lowerCount" if it's lower case. If it isn't
// a letter then leave it alone.
char rotate(int ch, unsigned upperCount, unsigned lowerCount)
{
    if (isupper(ch)) {
	ch = (ch - 'A' + upperCount) % 26 + 'A';
    } else if (islower(ch)) {
	ch = (ch - 'a' + lowerCount) % 26 + 'a';
    }
    return ch;
}


int
main()
{
    string word("hello World!");
    // rotate the characters in word
    for (auto &ch: word) {
	ch = rotate(ch, 5, 9);
    }
    cout << word << '\n';
}


Your code is bizarre. Why are you passing a and b as pointers? There's no reason for that. And why are you making it so it can only encode the string "Hello World"? That's retarded.

You should pass a and b as plain ints. And you should use isupper() and islower() to tell if the character is upper or lowercase.
Why are you passing a and b as pointers?
In his second post, the OP clearly states that this is required.
And why are you making it so it can only encode the string "Hello World"? That's retarded.
Maybe that 's just for testing, maybe it's part of the assignment. I wouldn't call it "retarded."
and you should use isupper() and islower()
Now that I agree with ;)
Topic archived. No new replies allowed.