manipulate array

alright i wrote this code it is a program that accept an array and manipulate it it do everything write but i have problem with case 7 in the switch statement i cant make the program accept another array without crashing
note:the array can be letters, numbers ,or symbols

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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
  #include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
using namespace std;

const int MaxChar = 81;

int CountWords(char string[MaxChar]);
int CountConst(char string[MaxChar]);
void JumbleString(char string[MaxChar],int length);

int main()
{
	int choice = 0;
	char string[MaxChar];
	char string2[MaxChar];
	char string3[MaxChar];
	
	
	int  start = 0, numofwords, numofconst;

	cout << "Enter a string: ";
	cin.getline(string, MaxChar, '\n');

	const int length = strlen(string);
	int end = length - 1;
	string3[length] = '\0';
	while (choice != 9)
	{
		cout << "USETHIS MENU TO MANIPULATE YOUR STRING" << endl
			<< "--------------------------------------" << endl
			<< "1) Inverse String"<<endl
			<< "2) Reverse string" << endl
			<< "3) To uprercase" << endl
			<< "4) Jumble string" << endl
			<< "5) Count number words" << endl
			<< "6) Count constants" << endl
			<< "7) Enter different string" << endl
			<< "8) print the string" << endl
			<< "9) Quit"<<endl;
		cin >> choice;

		switch (choice)
		{
		case 1:
			start = 0;
			while (string[start] != '\0')
			{
				if (islower(string[start]))
				{
					string[start] = toupper(string[start]);
				}
				else if (isupper(string[start]))
				{
					string[start] = tolower(string[start]);
				}
				start++;
			}
			cout << "the string is inversed" << endl;
			break;
		case 2:
			start = 0;
			for (; start != length; start++)
			{
				string2[start] = string[end];
				end = end - 1;
			}
			string2[length] = '\0';
			strcpy_s(string, string2);
			cout << "the string is reversed" << endl;
			break;
		case 3:
			start = 0;
			for (; string[start] != '\0'; start++)
			{
				string[start] = toupper(string[start]);
			}
			cout << "the string is changed to uppercase" << endl;
			break;
		case 4:
			JumbleString(string,length);
			break;
		case 5:
			numofwords = CountWords(string);
			cout << "the number of words is " << numofwords << endl;
			break;
		case 6:
			numofconst = CountConst(string);
			cout << "the number of constants is " << numofconst << endl;
			break;
		case 7:
			cout << "the other string is: ";
			cin >>string3;
			int l;
			l	=strlen(string3);
			string3[l] = '\0';

			strcpy_s(string, string3);
			break;
		case 8:
			cout << "the string is: " << string << endl;
			break;
		}
	}
	return 0;
}

int CountWords(char string[MaxChar])
{
	int numofwords = 0;
	int start = 0;
	for (; string[start] != '\0'; start++)
	{
		if (isspace(string[start+1])&&!isspace(string[start]))
		{
			numofwords++;
		}
	}
	if (string[0] != ' ')
	{
		numofwords++;
	}
	return numofwords;
}
int CountConst(char string[MaxChar])
{
	int numofconst = 0;
	int start = 0, end = strlen(string) - 1;
	for (; string[start] != '\0'; start++)
	{
		if (tolower(string[start]) == 'b' 
			|| tolower(string[start]) == 'd' 
			|| tolower(string[start]) == 'c'
			|| tolower(string[start]) == 'd' 
			|| tolower(string[start]) == 'f'
			|| tolower(string[start]) == 'g'
			|| tolower(string[start]) == 'h'
			|| tolower(string[start]) == 'j'
			|| tolower(string[start]) == 'k'
			|| tolower(string[start]) == 'l'
			|| tolower(string[start]) == 'm'
			|| tolower(string[start]) == 'n'
			|| tolower(string[start]) == 'p'
			|| tolower(string[start]) == 'q'
			|| tolower(string[start]) == 'r'
			|| tolower(string[start]) == 's'
			|| tolower(string[start]) == 't'
			|| tolower(string[start]) == 'v'
			|| tolower(string[start]) == 'w'
			|| tolower(string[start]) == 'x'
			|| tolower(string[start]) == 'y'
			|| tolower(string[start]) == 'z')
		{
			numofconst += 1;
		}
	}
	return numofconst;
}
void JumbleString(char string[MaxChar],int length)
{
	char stringj[MaxChar];
	stringj[length] = '\0';
	int value[MaxChar]; //array to store the random numbers in
	value[length] = '\0';
	srand(time(NULL)); 
	int i = 0, j = 0;
	bool check; //variable to check or number is already used
	int n; //variable to store the number in
	//generate random numbers:
	for (; i<length; i++)
	{
		
		do{
			n = rand() % length;
			//check or number is already used:
			check = true;
			for (j=0; j < length; j++)
			{
				if (n == value[j]) //if number is already used
				{
					check = false; //set check to false
					break; //no need to check the other elements of value[]
				}
			}
		} while (!check); //loop until new, unique number is found
		value[i] = n; //store the generated number in the array
		stringj[i] = string[n];
	}
	cout << "the jumbled string is: " << stringj << endl;
}
Actually your program is not crashing, it gets into an endless loop.
Try this:
1
2
3
4
5
6
7
8
9
case 7:
  cout << "the other string is: ";
  cin >> string3;
  cin.ignore(1024, '\n');
  int l;
  l =strlen(string3);
  string3[l] = '\0';
  strcpy_s(string, string3);
  break;

\
Thanks very much. that helped a lot i fixed it

1
2
3
4
5
6
7
8
9
case 7:
			cout << "the other string is: ";
			cin.ignore();
			cin.getline(string2,MaxChar,'\n');
			int l;
			l	=strlen(string2);
			string3[l] = '\0';
			strcpy_s(string, string2);
			break;
Last edited on
Topic archived. No new replies allowed.