Hello everybody,
I am trying to write a program consisting of two parts:
1. It lists of letters before and after a certain letter (if the user requested 2 letters regarding 'e', it would be cd and fg)
2. count the number of letters in each word of a sentence contained in a character array (word1 is xx letters, word2 is xx letters, etc.)
So far I have found a way a to find part 2, and have also found a better way of doing it using a string array (which to me seems much simpler). I was wondering if there is a tidier way of using a character array.
And for part 1, I am totally stumped. I am leaning to using a char array for the alphabet, and thats about it.
Here is my code
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
//prototypes
void printNeighbors(int neighbor);
//string array way
//void printWordLengths(char sentence1[]);
void printWordLengths(char sentence1[], char sentence2[], char sentence3[], char sentence4[], char sentence5[]);
int main ()
{
char choice;
int count;
do
{
cout << setfill ('-')
<< setw (40)
<< "-"
<< setfill (' ')
<< endl;
cout << "N[eighbors]"
<< setw (10)
<< "W[ords]"
<< setw (10)
<< "Q[quit]"
<< endl;
cout << "Enter your choice --> ";
cin >> choice;
if (choice == 'n' || choice == 'N')
{
cout << "Enter the letter of numbers to show: ";
cin >> count;
printNeighbors(count);
}
else if (choice == 'w' || choice == 'W')
{
//These classes are really, really easy!
//character array way of doing it
char These [] = "These";
char classes [] = "classes";
char are [] = "are";
char really [] = "really";
char easy [] = "easy";
printWordLengths(These, classes, are, really, easy);
//string array of doing it
//string troll[6] = {"These", "classes", "are", "really", "really", "easy"};
//printWordLengths(troll);
}
}
while (choice != 'q' && choice != 'Q');
cout << "Bye!" << endl;
system ("pause");
return 0;
}
void printNeighbors(int neighbor)
{
//for each letter between ’e’ and ’j’ (inclusive)
//print the COUNT letters before and after each letter
//char alphabet[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'};
cout << "The " << neighbor << " neighbors of 'e' are:" << endl;
cout << "The " << neighbor << " neighbors of 'f' are:" << endl;
cout << "The " << neighbor << " neighbors of 'g' are:" << endl;
cout << "The " << neighbor << " neighbors of 'h' are:" << endl;
cout << "The " << neighbor << " neighbors of 'i' are:" << endl;
cout << "The " << neighbor << " neighbors of 'j' are:" << endl;
}
//string array
//void printWordLengths(char sentence[])
void printWordLengths(char sentence1[], char sentence2[], char sentence3[], char sentence4[], char sentence5[])
{
//string array
//for(int i = 0; i < 6; i++)
//{
// cout << "Word #"<< i+1 << " is " << sentence[i].size() << " characters." << endl;
//}
cout << "Word #1 is " << strlen(sentence1) << " characters." << endl;
cout << "Word #2 is " << strlen(sentence2) << " characters." << endl;
cout << "Word #3 is " << strlen(sentence3) << " characters." << endl;
cout << "Word #4 is " << strlen(sentence4) << " characters." << endl;
cout << "Word #5 is " << strlen(sentence5) << " characters." << endl;
return;
}
Thanks.