I am stuck at working out what is wrong with my code. I receive the values entered at the command line, I ask the user for text, I validate the command line values as characters of the alphabet and that they number 26 and are all different.
When I try to substitute the user entered text with the code characters entered at the command line I get the Segmentation error.
string cypherText(string s, string t)
{
string substitutionKey = t;
string unencodedPhrase = s;
string encodedPhrase = NULL;
string alphabetLower = "abcdefghijklmnopqrstuvwxyz";
string alphabetUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for(int i = 0; i < strlen(unencodedPhrase); i++)
{
for (int j = 0; j < 26 ; j++)
{
if (unencodedPhrase[i] == alphabetLower[j])
{
encodedPhrase[i] = substitutionKey[j];
}
}
}
return encodedPhrase;
}
The value passed in from the main section is the string that the user entered
string outPut = cypherText(inputPhrase, substitutionKey);
string substitutionKey = argv[i];
The substitutionKey is the string entered at the command line. The error checking to ensure the substitutionKey is all aphabetical and different values works.
The segmentation error occurs at this line
encodedPhrase[i] = substitutionKey[j]
The for loop is intended to loop through the user entered text to encode, starting at position 0, and find the character match in the command line entry and then put the relevant character form the command line entry into the new encoded phrase, and then move to the next character and so on until the end of the user entered text word sentence etc.
Any advice is appreciated as to the remedy.