Question

I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int...

I need to update this code:


#include <iostream>
#include <string>
#include <cctype>
using namespace std;

int main()
{
string s;
cout<< "Enter a string" <<endl;
getline (cin,s);
cout<< s <<endl;
int vowels=0,consonants=0,digits=0,specialChar=0;

for (int i=0; i<s.length(); i++) {
char ch=s[i];
if (isalpha(s[i])!= 0){
s[i]= toupper(s[i]);   
if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
else if (isdigit(s[i])!= 0)
digits++;
else
specialChar++;

}
cout<<"Vowels="<<vowels<<endl;
cout<<"Consonants="<<consonants<<endl;
cout<<"Digits="<<digits<<endl;
cout<<"Special Characters="<<specialChar<<endl;
return 0;
}

so that it accomplishes everything below:


Read your string from an input file instead of from the user
Need it to refernce a a file named "words.txt"
Remove the call to getUserInput. You can also remove its definition and prototype if you like.


Then have it process all of the words in the file, not just a single string
In your main function, add a loop to read and print counts for all the words in the file.
Here is what the loop condition would look like if you were reading from an input file variable named myinputfile into a string named word:
while (myinputfile >> word)

The input within this loop test should be the only input statement in your code. Remove any other myinputfile >> word statements
Change your report function and any other necessary code so that each word gets printed like this.
c++is2xasfunasPython! V=5 C=12 D=1 S=3

You should have 2 spaces before each counter. Here's an example with a 2-word file:
c++is2xasfunasPython! V=5 C=12 D=1 S=3
blah V=1 C=3 D=0 S=0

And finally You'll sort all the strings in the file
Finally, you are going to sort the words in the file (alphabetically, according to the ASCII table) before you print them.
This means that you will have to read the words into an array, sort the array, and then print the reports for each word.
eorganize your code so that:

In one loop, you read the words into an array instead of a single string. You will want to add a new variable to keep track of the number of words in the array. A safe maximum size for the array is 1000.
In a second loop, you go over each array element and compute and print the counts.
add the following function definitions to the bottom of your code, and add prototypes for them at the top. Before each definition, write a comment explaining what the function does

void Swap(string& a, string& b) {
string s = a;
a = b;
b = s;
}

void Sort(string W[], int N) {
for (int i = 1; i < N; i++) {
int j = i;
while (j >0 and W[j] < W[j - 1]) {
Swap(W[j], W[j-1]);
j--;
}
}
}


In between your two loops in main(), call the Sort function to sort the elements of your array.
Here's the output you should see:

15 V=0 C=0 D=2 S=0
It V=1 C=1 D=0 S=0
This V=1 C=3 D=0 S=0
and V=1 C=2 D=0 S=0
characters! V=3 C=7 D=0 S=1
contains V=3 C=5 D=0 S=0
few V=1 C=2 D=0 S=0
file V=2 C=2 D=0 S=0
has V=1 C=2 D=0 S=0
letters V=2 C=5 D=0 S=0
lots V=1 C=3 D=0 S=0
of V=1 C=1 D=0 S=0
special V=3 C=4 D=0 S=0
very V=1 C=3 D=0 S=0
words. V=1 C=4 D=0 S=1

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code :

#include <iostream>
#include <string>
#include <cctype>
#include <fstream> // header file for handling files
using namespace std;
// swap 2 string
void Swap(string& a, string& b) {
string s = a;
a = b; b = s;
}
// sort the array paased as argument in increasing order
void Sort(string W[], int N) {
for (int i = 1; i < N; i++) {
int j = i;
while (j >0 and W[j] < W[j - 1])
Swap(W[j], W[j-1]) ,j--;
}
}

int main(){
// my input file
ifstream myfile;
string words[1000];
string word;
myfile.open("input.txt"); // my input file is input.txt you can change it as required
int k = 0;
// read words from file atmost 1000 words
while(myfile>>word && k < 1000)
words[k] = word , k++;
// sort the array
Sort(words , k);
// iterate over every word
cout<<k<<endl;
for(int j = 0; j < k ; j++){
word = words[j];
int vowels=0,consonants=0,digits=0,specialChar=0;

for (int i=0; i<word.length(); i++) {
if (isalpha(word[i])){
// convert the word[i] to lower case
char ch = tolower(word[i]);
if (ch == 'a'|| ch == 'e'|| ch == 'i'|| ch == 'o' || ch == 'u')
vowels++;
else
consonants++;
}
else
if(isdigit(word[i]))
digits++;
else
specialChar++;
}
// print the word and then the required data
cout<<word;
cout<<" V = "<<vowels;
cout<<" C = "<<consonants;
cout<<" D = "<<digits;
cout<<" S = "<<specialChar;
cout<<endl;
}

}

Sample Run :

Code Image :

Add a comment
Know the answer?
Add Answer to:
I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT