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.

Recommended Answers

All 2 Replies

Few Problems I See with is Are: words with multiple times same letter, so im guessing itll be print every single time the letter pops up so function should go aline of this:

char sentence[x];

so what you'd do is 1 have a for loop 2 count the letters checked.

//you'll need to pass the strings you want to check into here.

char LetterUserWantsToTest[1];
cin >> LetterUserWantsToTest >> endl;

for (int i=0; foundnull == 0; i++){ //scans until foundnull != 0
 if(strcmp(sentence1[i],LetterUserWantsToTest) == 0){
  std::cout << sentence1[i-1];
  std::cout << sentence1[i+1];
 if(stentence[i] == NULL){
   foundnull=1; //exits the for loop once we hit Ending of String.
 }
}

So Basicly Let me explain: Letteruserwantstotest is simply the letter the user would like to search
as the middle letter of the word / sentence.

strcmp function is a built in c function, simply:

(strcmp(word1,word2) == 0); if word1 matchs word2 and their is no difference their is a match.
0 means their are no differences between word1 and word 2.
But we are basicly checking sentence1[postition of the array] to the letter used. then print letters
before senctence[i-1]; and after sentence[i+1];


Things To Add:

#include <string.h> in top of file to be able to use strcmp.

also you'll need to pass the sentences into the function that you'll be using (or what i wrote, wasnt tested due to fact it was writen on notepad).

Last thing i may sugguest. if in the for loop i is equal to zero dont print the sentence1[i-1] because you will go outa scope of the program and most likely generate garabage.


Any mistakes on my part are OPPS :( but that should defently help you on your way.

Sorry I should have been clearer, but the requirements are to just find out the word lengths of a built in sentence contained in a char array, requiring no user input except for w.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.