Characters and String Functions

Desired output: https://app.box.com/s/r47o73hk1xix9gm0svdl
Hi. I've been sitting here and searching everywhere how to solve this and i can't. Please help me.


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
//The wrong code here is the present number of times
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;

char ltr;
string phrase;


int main()
{
	cout << "Enter a string: ";
	getline(cin, phrase);
	cout << "Enter character to search: ";
	ltr = isalpha(phrase[1000]);
	cin >> ltr;
	cout << "The given string is: " << phrase;
	cout << "\nGiven character is " << ltr;
	cout << "\nPresent number of times: " << strlen(isalpha(phrase[1000]));
	//for (int i = 0; i < strlen(phrase); i++){
	//		cout << isalpha(phrase[i]);
	//}

	system("pause>0");
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//this is a fixed code.. no need to change
#include<iostream>
#include<string>
using namespace std;
string phrase;
int numspaces = 0;
char nextChar;
int main()
{
	cout << "Enter a line of text: ";
	getline(cin, phrase);
	cout << "Given string: " << phrase;
	for (int i = 0; i<int(phrase.length()); i++)
	{
		nextChar = phrase.at(i); 
		if (isspace(phrase[i]))
			numspaces++;
	}
	cout << "\nNumber of words: " << numspaces+1;


	system("pause>0");
	return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//ugh the hardest... :/
#include<iostream>
#include<string>
using namespace std;
char phrase[1000];
int n(0);

int main()
{
	cout << "Enter a string: ";
	cin >> phrase;
	cout << "\n";
	cout << "No. of uppercase: ";
	for (int i = 0; i < strlen(phrase); i++){
		if (isupper(phrase[i]))
			n++;
	}
	cout << n;
	
	system("pause>0");
	return 0;
}
Last edited on
//solved the first output the only remaining one is the last one
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
#include<iostream>
#include<cstring>
#include<algorithm>
#include<string>
using namespace std;

char ltr;
char phrase[1000];
int countChars(char *, char);


int main()
{
	int count = 0;
	cout << "Enter a string: ";
	cin.getline(phrase, 1000);
	cout << "Enter character to search: ";
	cin >> ltr;
	cout << "The given string is: " << phrase;
	cout << "\nGiven character is " << ltr;
	cout << "\nPresent number of times: " << countChars(phrase, ltr);


	system("pause>0");
	return 0;
}

int countChars(char *strPtr, char ch)
{
	int times = 0;
	while (*strPtr != '\0')
	{
		if (*strPtr == ch)
			times++;
		strPtr++;
	}
	return times;
}

What's the problem you are having?
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
#include<iostream>
#include<string>
using namespace std;
char phrase[1000];
int n(0), k(0), d(0), e(0), f(0);

int main()
{
	cout << "Enter a string: ";
	cin.getline(phrase, 1000);
	cout << "\n";
	cout << "No. of uppercase: ";
	for (int i = 0; i < strlen(phrase); i++){
		if (isupper(phrase[i]))
			n++;
	}
	cout << n;
	
	cout << "\nNo. of lowercase: ";
	for (int i = 0; i < strlen(phrase); i++){
		if (islower(phrase[i]))
			k++;
	}
	cout << k;

	cout << "\nNo. of digits: ";
	for (int i = 0; i < strlen(phrase); i++){
		if (isdigit(phrase[i]))
			d++;
	}
	cout << d;
	
	cout << "\nNo. of others: ";
	for (int i = 0; i < strlen(phrase); i++){
		if (ispunct(phrase[i]))
			e++;
	}
	for (int i = 0; i < strlen(phrase); i++){
		if (isspace(phrase[i]))
			f++;
	}
	cout << e+f;

	system("pause>0");
	return 0;
}

fixed the last one aye
Topic archived. No new replies allowed.