Below is the project specs for the program I am writing in C++ (I have also incl
ID: 3538929 • Letter: B
Question
Below is the project specs for the program I am writing in C++ (I have also included the code I've done so far). I am trying to figure out how to do the arrays to get this to work. Any suggestions would be greatly appreciated.
Write a program that will, when given a specific word length as input, find all words generated by shifting each letter in the word by the same amount of letters. You should find all words generate by shifting by one letter (as in the case of HAL -> IBM), for 2 letters, and so on up to 25 letters. Your program should be able to handle word lengths of a minimum of 3 and a maximum of at least 7.
So, if a user types in 3 for the word length, the program should take the first three-letter word in the dictionary, shift every letter by 1, and see if the resulting combination of letters is also a word in the dictionary. Then it should shift each letter by 2, then 3, and so on to 25 (you have to figure out how to wrap around at the end of the alphabet). Then the program will proceed to the next word in the dictionary file and repeat the procedure - and so until every three-letter word in the dictionary file has been used to generate shifted words.
For example, bob generates ere when shifted by 3 letters and huh when shifted by 6 letters.
Here's the code I have written so far:
#include <iostream>
#include <fstream> // To read into a file
#include <string>
using namespace std;
int length() // function to retrieve the maximum number of characters from the user.
{
int maxLength; // the max number of characters the user wants the word length to be
cout << "What is the word length? ";
cin >> maxLength;
//cout << "Word length is " << maxLength << " " << " " ;
return(maxLength);
}
string conversion() // function to convert the letters of the word
{
string newAlpha;
return(newAlpha);
}
int main()
{
int numOfChars; // the maximum number of characters the word can be
numOfChars = length(); // retrieving value from function length()
string wordBreakdown[3];
string input;
fstream inputStream; // setup the call to the text file
inputStream.open("words.txt"); // calling the text file
while (inputStream >> input)
{
if (input.length() == numOfChars)
{
cout << "Word found is: " << input << " ";
string my_string = input;
int len = my_string.length(); // or .size();
string theLetter;
int i;
for(i = 0; i < my_string.length(); i++)
{
cout << "The count is " << i << " and the string value is " << my_string[i] << " ";
//char *wordBreakdown[numOfChars];
wordBreakdown[i] = my_string[i];
theLetter = wordBreakdown[i];
cout << "wordBreakdown is " << wordBreakdown[i] << " and the letter is " << theLetter << " ";
theLetter = conversion();
}
char alphaChar[26] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
}
}
inputStream.close(); // close the text file
cout << " " << "Enter a character to exit." << " ";
char wait;
cin >> wait;
return 0;
}
Explanation / Answer
Better to add through ASCII that would mean 'a' + 1 = 'b' rather than the char array alphaChar[]
Algo. can be something like:
char word[user_length];
int j=1;
while(j<=25)
{
for(int i=0;i<user_length;i++)
{
if(word[i] == 'z')
word[i] = 'a';
else
word[i] = word[i] + 1;
}
if(word not in dictionary)
add to dictionary
j++;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.