Help needed with assignment C++ (Beginner level)

Hey guys ...I'm in Computer science class that i need for my engineering major. We went over basics of computer science, and below is the assignments that our professor assigned us. I've really tried hard to understand it but i could nt figure out whats wrong with it.
//* Write a C++ program that creates a list of letters that will *
//* be used to encode a message according to the following rule: *
//* *
//* 1. Input a WORD *
//* 2. Remove all repeating letters to form the modified WORD *
//* 3. Place the modified WORD at the beginning of the array *
//* 4. Fill the remainder of the list with any letters of the *
//* alphabet that were not used in the WORD working from A to *
//* Z. (Your list should have all 26 letters of the alphabet *
//* *
//* EXAMPLE: If the user entered HELLO *
//* the modified WORD would become HELO *
//* *
//* and the list would become: *
//* HELOABCDFGIJKMNPQRSTUVXYZ *
//* *
//* NOTE: The list must be stored in an array of 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
33
34
#include<iostream>
using namespace std;
int main()
{
	char a;
	int b = 0;
	char word[4] = "\0";
	char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char code[27];
	cout << "Please enter a word:" << endl;
	cin >> word;
	for (int i = 0; i<3; i++)
	{
		if (word[i] == word[i - 1])
		{
			a = word[i];
			word[i] = word[i + 1];
		}
		code[i] = word[i];
		b++;
	}
	for (int o = 0; o<27; o++)
	{
		if (alphabet[o] == word[1] || alphabet[o] == word[2] || alphabet[o] == word[3] || alphabet[o] == word[0])
		{

			o++;
		}
		code[b] = alphabet[o];
		b++;
	}
	cout << code;
	return 0;
}


AND PLEASE IF YOU CAN'T HELP DONT LEAVE NEGATIVE COMMENTS!! THANK YOU!!
I'll give some tips.

1 - Go through all the "word" instead of 3 char away.
2 - Check if the input word in "word" has any repeated char. Check it by doing one loop inside another about the "word".
3 - Chars whose are repeating, store it somewhere else, then, delete from "word".
4 - Regroup the array "word" to the start and add again those chars.
5 - Go through the "word" and check any char whose is in "alphabet". If so, skip them, otherwise, add them to the end.

Don't forget to raise the size of "word". Just to make it clear, I'm considering the variable "word", as the main variable, which the user will put their word in.
Last edited on
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
int _tmain(int argc, _TCHAR* argv[])
{
	//HELLO
	char word[5] = "\0";			
	char *wordR= new char[5];			//result string
	char alphabet[27] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	char code[27];
	int j=0;
	int k=0;
	int j1=0;

	cout << "Please enter a word:" << endl;
	cin >> word;
	
	for(int j =0 ;j<5;j++)
	{
		for(int i=0;i<27;i++)
		{
			if(word[j]==alphabet[i])					//check for string mached between word and alphabet
			{
				k=i;
				while(alphabet[k]!='\0')				//which char mached , delet from alphabet
				{
					alphabet[k] = alphabet[k+1];		//char in alphabet shift by one.
					k++;
				}
				wordR[j1++] = word[j];					//which char matched stored into Result string
				break;
			}

		}
		
	}
	wordR[j1++] = '\0';									//insert NULL char to terminate string
	
	cout<<"\n\n";
	cout<<wordR<<" "<<alphabet<<endl;					//print final string

	getch();
	return 0;
}


Hi may be this is not very efficient way to do but it will work.
@ak16 Thanks! i have nt learned to do it this way.Its works great! Thank you!
Topic archived. No new replies allowed.