c++ program..please help me to solve this

Q. Write a program that performs the following operations on strings:
• Use a function count(char[]) to count the number of no. of vowels (uppercase & lowercase) & the no. of consonant ( uppercase & lowercase) no. of spaces, no. words, no. of digits and the no. of special characters in the string.
• Use a function change() to change the case of characters of a string.
• Uses a function reverse() to find the reverse of a string.
• Uses a function replace() to replace every space in a string with a hyphen.
• Uses a function check () to check whether a character is present in the string or not. The function should take in the string and the character to be searched and then display appropriate message.
• Uses a function revword() to reverse each word of the string.
This program should be pretty easy to write if you can use the <cctype> header file.
Are you allowed to use that header file?
If so, it has all the necessary functions needed to check for words,digits,symbols.
Take a look here:
http://www.cplusplus.com/reference/cctype/

Then you can just make if statements to check for certain scenarios.
Like
if(isdigit)
digitcounter++;

Secondly you're going to use for loops for everything.
Because you want to look at each character in a string.

That reminds me, what is your input going to be?
Are you reading in words per line in a file or what?

If for example the input was something like this:
This sentence has spaces and the numbers 123 with @# symbols.


Do you want to treat the entire line as one big string or take each element separated by white space as individual strings?

If the whole sentence was 1 giant string then use the getline function to read in the entire line.

So if I wanted to count the number of digits in that string, I would do something like this:

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
#include <iostream>
#include <string>
#include <cctype>
using namespace std;

void check_digits(string)

int main()
{
string phrase;
cout << "Please enter a phrase " << endl;
cin >> phrase;

check_digits(phrase);

return 0;
}

void check_digits(string word)
{
int digcount = 0;
n = word.length(); // finds length of word;
for (int i = 0; i < n; i++)
{
if(isdigit(word[i]))
digcount++;
}
cout << "There are " << digcount << "digit(s) in "<< word << endl;
}
Topic archived. No new replies allowed.