Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

here my code using C++ string not c string #include <iostream> #include <string>

ID: 3642175 • Letter: H

Question

here my code using C++ string not c string


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

string countWords(string);
string countConsonants(string);

int main()
{
string word;
char cword;
word = cword;
char Selection;
string coWords;
string coConsonants;


//the user enters a word, sentence or a string of numbers
cout << "Please enter a word, sentence, or a string of numbers ";
getline(cin, word);


do
{


cout << word;

cout << "USE THIS MENU TO MANIPULATE YOUR STRING ";
cout << "---------------------------------------- ";
cout << "1) Inverse String ";
cout << "2) Reverse String ";
cout << "3) To Uppercase ";
cout << "4) Count Number Words ";
cout << "5) Count Consonants ";
cout << "6) Enter a Different String ";
cout << "7) Print the String ";
cout << "Q) Quit ";
cin >> Selection;
cin.ignore();

if(Selection == '1' )// here it suppose to inverse the upper cases and lower cases
//of the string and numeric characters or speacial characters do not change
{
for(int i; i < word.length(); i++)
{
}



}
else if(Selection == '2')
{
for(int i = word.length()-1; i >=0; i--)
{

//not sure if its rigth but its suppose to
//to reverse the order of the characters of the string
//like from 2020 to 0202
}
}
else if(Selection == '3')
{
for(int i = word.length(); i >=0l;)
{
// here its suppose to convert all charactes in to uppercase
// numeric characters dont change as well as speacial characters
(char)(toupper)(cword);
}
}
else if(Selection == '4')
{
coWords = countWords(coWords); // suppose to be a function that
// counts how many words there are not letters.
//NO void

cout << "There are " << coWords << " words ";
}
else if(Selection == '5')
{
coConsonants = countConsonants(coConsonants);
// a function that suppose to count how many consonants not VOID
}
else if(Selection == '6')
{
//here the user suppose to enter another string
// this replaces the orginal one
}
else if(Selection == '7')
{
//here its suppose to display messgae from the selections
//AN example: So if the original string was

Explanation / Answer

You had a good start, but there were a few errors... I fixed the errors and added the code for missing functions. All changes are commented.

#include <iostream>

#include <string>

#include <cctype>

using namespace std;

int countWords(string);       // these are both count functions, so they should return

int countConsonants(string); // a number (int), not a string

int main()

{

      string word;

      string reverse; // need this for the reverse function

      // char cword; // Don't know what this char was for

      // word = cword; // word is a string and cword is a character...

                      // not sure what this is supposed to do!

      char Selection; // menu selection character

      int coWords;      // the counts will be numbers

      int coConsonants; // not strings -- changed type to int

      //the user enters a word, sentence or a string of numbers

      cout << "Please enter a word, sentence, or a string of numbers ";

      getline(cin, word);

      do

      {

            cout << " USE THIS MENU TO MANIPULATE YOUR STRING ";

            cout << "---------------------------------------- ";

            cout << "1) Inverse String ";

            cout << "2) Reverse String ";

            cout << "3) To Uppercase ";

            cout << "4) Count Number Words ";

            cout << "5) Count Consonants ";

            cout << "6) Enter a Different String ";

            cout << "7) Print the String ";

            cout << "Q) Quit ";

            cin >> Selection;

            cin.ignore();

            if(Selection == '1' ) // here it suppose to inverse the upper cases and lower cases

                                    //of the string and numeric characters or speacial characters do not change

            {

                  for (int i = 0; i < word.length(); i++) // loop from 0 to word.length()

                  {

                        if (isupper(word[i])) // is the current character uppercase?

                        {

                              word[i]=tolower(word[i]); // yes, convert to lowercase

                        }

                        else if (islower(word[i])) // is the current character lowercase?

                        {

                              word[i]=toupper(word[i]); // yes, convert it to uppercase

                        }

                  }

            }

            else if(Selection == '2')

            {

                  reverse.clear(); // clear the reverse string

                  for(int i = word.length()-1; i >=0; i--) // loop backwards from word.length() to 0

                  {

                        //not sure if its rigth but its suppose to

                        //to reverse the order of the characters of the string

                        //like from 2020 to 0202

                        reverse.append(word,i,1); // append character from word to reverse

                  }

                  word = reverse; // assign reverse back to word

            }

            else if(Selection == '3')

            {

                  for (int i = 0; i < word.length(); i++) // loop from 0 to word.length()

                  {

                        // here its suppose to convert all charactes in to uppercase

                        // numeric characters dont change as well as speacial characters

                        //(char)(toupper)(cword);

                        if (islower(word[i])) // is this a lowercase character?

                        {

                              word[i] = toupper(word[i]); // yes, convert to uppercase

                        }

                  }

            }

            else if(Selection == '4')

            {

                  coWords = countWords(word); //call function to count words in string word

                  // suppose to be a function that

                  // counts how many words there are not letters.

                  //NO void

                  cout << "There are " << coWords << " words ";

            }

            else if(Selection == '5')

            {

                  coConsonants = countConsonants(word); // call function to count consonants in string word

                  // a function that suppose to count how many consonants not VOID

                  cout << "There are " << coConsonants << " consonants ";

            }

            else if(Selection == '6')

            {

                  // here the user suppose to enter another string

                  // this replaces the orginal one

                  cout << "Please enter new word, sentence, or a string of numbers ";

                  getline(cin, word);

            }

            else if(Selection == '7')

            {

                  //here its suppose to display messgae from the selections

                  //AN example: So if the original string was “Hello” and the user processed the string with option 3 followed by option 2,

                  //followed by option 8, the string would print out as “OLLEH”

                  cout << word << endl;

            }

      } while(Selection != 'Q' && Selection != 'q'); //accept uppercase Q or lowercase q to quit

      system("PAUSE");

      return 0;

}

int countWords(string s)

{

      int count; // variable to hold word count

      if (s.empty())

      {

            return 0; // return 0 wordcount if string is empty

      }

      else

      {

            count = 1; // if string is not empty, initialize count to 1

      }

      for (int i = 0; i < s.length(); i++) // loop from 0 to s.length()

      {

            if (isspace(s[i]) && !isspace(s[i+1])) // a space followed by a nonspace delimits a word

            {  

                  count++; // increment the word count

            }

      }

      return count; // return count

}

int countConsonants(string s)

{

      // declare a static string of consonants to search

      static string consonants = "bcdfghjklmnpqrstvwxyzBCDFGHJKLMNPQRSTVWXYZ";

      int count = 0; // start with count = 0

      size_t found; // flag for searching

      for (int i = 0; i < s.length(); i++) // loop from 0 to s.length()

      {

            found = consonants.find(s[i]); // look for current character in consonants string           

            if (found!=string::npos) // was it found?

            {

                  count++; // yes, increment count

            }

      }

      return count; // return count

}