functions and length of arrays

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

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114

#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.
with characters, they can be treated like numbers

So you can do char var = 'e';

var++; //equals f
var--; // equals d
etc

hope this helps
Last edited on
Thanks for the tip.

I would then apply an accumulator with the chars? Or could I store them into an array?
you can store them in an array if u need to

I would personally take the char, do a loop printing them...

for instance

somechar = somechar - n;
for(int i = 0; i < n; i++){
cout << somechar++ << end;
}

it will take the lowest value needed, then increment printing for it continuously until it reached its destination...

alpha = alpha-neighbor;
for(int i=0; i<neighbor; i++)
{
cout << alpha++;

}

I've set alpha as e in main, and got it to print out abcd if user enters 4, but how do you place abcd in cout << "The " << neighbor << " neighbors of 'e' are:" << endl;
if you want, you can store the list into an array if you want them all to print out together,

I have a dilemma for chars f through j. var2 is changed by //e by var2++. I need it to stay f after doing //e. How do I return var2 to its f value?

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
void printNeighbors(int neighbor)
{
	//for each letter between ’e’ and ’j’ (inclusive)
	//print the COUNT letters before and after each letter
	string arrayStore [9001];
	//e
	char var1 = 'e';
	char var2 = 'f';
	char var3 = 'g';
	char var4 = 'h';
	char var5 = 'i';
	char var6 = 'j';
	var1 = var1 - neighbor;
		var2 = var2 - neighbor;

	

	for(int i=0; i<neighbor; i++)
		{
			//e 
			arrayStore[1] = arrayStore[1] + var1++;
			arrayStore[2] = arrayStore[2] + var2++;
			//f
			arrayStore[3] = arrayStore[3] + var2++;

			

		}


	cout << "The " << neighbor << " neighbors of 'e' are:" << "\t" << arrayStore[1] << " and " << arrayStore[2] << endl;
	
	return;
}
Last edited on
Yes i got it. In order to go back to f for var 2, I multiplied neighbor by 2 so it would start at f.

but i had to create 5 more for statements, and this is what i put before the for statement for f:
var2= var2-2*neighbor
resets var2 back to f

Thanks oonej!
Last edited on
also, i used strtok_s instead of doing char for each word, so it is much cleaner now
Topic archived. No new replies allowed.