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
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 :
I need to update this code: #include <iostream> #include <string> #include <cctype> using namespace std; int...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
Please add a detailed comment for this program. #include<iostream> #include<string> #include<fstream> #include<sstream> #include<cctype> using namespace std; int is_palindrome(string word){ int len = word.size(); for(int i=0; i<len/2; i++){ if(toupper(word[i])!=toupper(word[len-i-1])) return 0; } return 1; } int have_vowels3(string word){ int cnt = 0; for(int i=0; i<word.size(); i++){ if(tolower(word[i])=='a' || tolower(word[i])=='e' || tolower(word[i])=='i' || tolower(word[i]) =='o' || tolower(word[i]) == 'u') cnt++; } if(cnt>=3) return 1; else return 0; } int have_consecutives(string word){ for(int i=0; i<word.size()-1; i++){ if(tolower(word[i])=='o' && tolower(word[i+1]=='o')) return 1; } return...
I need help with this code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void dump(int ar[], int size)
{
cout << "\nDUMP [ ";
for (int i=0; i<=size; i++)
{
cout << ar[i] << " ";
}
cout << "]\n\n";
}
void quicksort(int ar[], int start, int end)
{
cout << "TOP OF SORT ===========================" <<
endl;
dump(ar, start, end);
int pivot = ar[end];
int left = start;
int right = end - 1;
int tmp;
cout <<...
#include #include #include #include #include #include #include using namespace std; const int MAX = 26; string addRandomString(int n) { char alphabet[MAX] = { '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' }; string res = ""; for (int i = 0; i < n; i++) res = res + alphabet[rand() % MAX]; return res;...
***************Fix code recursive function #include <iostream> #include <cctype> #include <string> using namespace std; void printUsageInfo(string executableName) { cout << "Usage: " << executableName << " [-c] [-s] string ... " << endl; cout << " -c: turn on case sensitivity" << endl; cout << " -s: turn off ignoring spaces" << endl; exit(1); //prints program usage message in case no strings were found at command line } string tolower(string str) { for(unsigned int i = 0; i < str.length(); i++)...
can you please split this program into .h and .cpp file #include <iostream> #include<string> #include<fstream> #define SIZE 100 using namespace std; //declare struct struct word_block { std::string word; int count; }; int getIndex(word_block arr[], int n, string s); int main(int argc, char **argv) { string filename="input.txt"; //declare array of struct word_block word_block arr[SIZE]; int count = 0; if (argc < 2) { cout << "Usage: " << argv[0] << "...
How do can I update this code (Code A): Code (A) #include using namespace std; int fibonacci(int n) { int a = 0, b = 1, c; if (n <= 1) return n; for (int i = 2; i <= n; i++) { c = a + b; a = b; b = c; } return b; } int fibonacciRecursive(int n) { if (n <= 1) { return n; } return fibonacciRecursive(n-1) + fibonacciRecursive(n-2); } int main() { int n;...
#include <iostream> #include <string> using std::string; using std::cout; using std::endl; void testAnswer(string testname, int answer, int expected) { if (answer == expected) cout << "PASSED: " << testname << " expected and returned " << answer << "\n"; else cout << "FAILED: " << testname << " returned " << answer << " but expected " << expected << "\n"; } // Implement printArray here void printArray(int array[],int b) { for(int i = 0; i<b;i++) { std::cout<< array[i] << "...
#include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; struct transition{ // transition structure char start_state, to_state; char symbol_read; }; void read_DFA(struct transition *t, char *f, int &final_states, int &transitions){ int i, j, count = 0; ifstream dfa_file; string line; stringstream ss; dfa_file.open("dfa.txt"); getline(dfa_file, line); // reading final states for(i = 0; i < line.length(); i++){ if(line[i] >= '0' && line[i] <= '9') f[count++] = line[i]; } final_states = count; // total number of final states // reading...