Question

In: Computer Science

Write a C++ function to sort the characters in words in a inputed phrase. DO not...

Write a C++ function to sort the characters in words in a inputed phrase. DO not sort the characters in the string.

Some of the steps has been done for you. Fill out the program in C++. See below for the expected console output.

Make the program as simple as possible. Use introductory Object-Oriented C++ programming. If all the requirements are met, then this will be upvoted.

Do use the C++ Standard Library headers, do not use the C Standard Library headers (For instance, include cmath instead of math.h)

Do not use cstring’s or any of the cstring functions (strlen(), strcpy(), strcmp(), etc.)

Program:

#include

using namespace std;

string sortPhrase(string phrase){
  
  
}

int main(){
  
string sentence;
  
cout << "Enter a phrase: ";
  
getline(cin, phrase);
  
}

Expected Console Outputs:

Enter a phrase: Hello welcome to the exciting world of programming languages!  

The sorted phrase: Hello exciting languages! of programming the to welcome world

Enter a phrase: May the Force be with you!

The sorted phrase: Force May be the with you!   

Enter a phrase: There's no place like home.

The sorted phrase: There's home. like no place

Solutions

Expert Solution

The following is the code for above question. Since you were asked for the solution to be as simple as possible, i tried to keep it as simple as such, without using C Standard Library headers and also used basic c++ programming. This can also be done using other complex libraries also(vector,stringstream,strtok etc..).

#include<iostream>
using namespace std;
string sortPhrase(string phrase){  //function to sort the string and return it
  int i,j,k=0;
  string s[100],t,temp,sentence="";
  for(char x:phrase)  //split the string based on empty space and store the words in string array
  {
      if(x==' ')  //if space is found
      {
          s[k++]=t; //put the word before space into string array
          t=""; //Now make the string to ""
      }
      else
      {
          t=t+x; //else keep on adding each character till space is found
      }
  }
  s[k++]=t; //to put the last word
  for(i=0;i<k-1;i++)
  {
      for(j=i+1;j<k;j++)
      {
          if(s[i]>s[j]) //sorting the words 
          {
              temp=s[i];
              s[i]=s[j];
              s[j]=temp;
          }
          
      }
  }
  //Now convert the array of words to form a complete sentence by concatenating them
  for(i=0;i<k;i++)
  {
      if(i!=k-1) //if not last word
      {
          sentence=sentence+s[i]+" ";
      }
      else //if last word
      {
          sentence=sentence+s[i];
      }
  }
  return sentence; //return the sentence
  
}
int main()
{
    string phrase,sentence;
    cout<<"Enter a phrase: ";
    getline(cin,phrase); //input the phrase
    cout<<endl;
    sentence=sortPhrase(phrase); //sort the phrase
    cout<<sentence; //output the sorted phrase
    return 0;
}

I am also attaching the output and code screenshot for your reference.

Output and code screenshot:

#Please dont forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you


Related Solutions

Write a C program that counts the number of repeated characters in a phrase entered by...
Write a C program that counts the number of repeated characters in a phrase entered by the user and prints them. If none of the characters are repeated, then print “No character is repeated” For example: If the phrase is “full proof” then the output will be Number of characters repeated: 3 Characters repeated: f, l, o Note: Assume the length of the string is 10. ###Note: the output should print exactly as it is stated in the example if...
write a program in C Write a function that is passed an array of characters containing...
write a program in C Write a function that is passed an array of characters containing letter grades of A, B, C, D, and F, and returns the total number of occurrences of each letter grade. Your function should accept both lower and upper case grades, for example, both 'b' and 'B' should be bucketed into your running total for B grades. Any grade that is invalid should be bucketed as a grade of 'I' for Incomplete. You must use...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter,...
C++.how to write a selection sort to sort it. read_book_data() This member function takes one parameter, a string that contains the name of a file. This string parameter can be a C++ string or a C string (your choice). The function returns nothing. This constructor should do the following: Declare and open an input file stream variable using the file name string passed in as a parameter. Check to make sure the file was opened successfully. If not, print an...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their...
Write Insertion Sort and Bubble Sort Program for C# also write their algorithm and Explain their working.
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort,...
C++ --------------------------------------------- Do a comparison of a slow sort with Big O(n2) (selection sort, insertion sort, or bubble sort) and one faster sort of Big O(n * log n) (mergesort or quicksort). Count the number of moves (a swap counts as one move). With mergesort, you can count the range of the part of the array you are sorting (i.e. last-first+1). Use the code from the textbook (copy from the lecture notes) and put in an extra reference parameter for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for...
Write a program in C++ to test either the selection sort or insertion sort algorithm for array-based lists as given in the chapter. Test the program with at least three (3) lists. Supply the program source code and the test input and output. List1: 14,11,78,59 List2: 15, 22, 4, 74 List3: 14,2,5,44
write this program in C++ Write a program that prompts a user for three characters. The...
write this program in C++ Write a program that prompts a user for three characters. The program must make sure that the input is a number 10 - 100 inclusive. The program must re prompt the user until a correct input is entered. Finally output the largest and the lowest value. Example 1: Input : 10 Input : 20 Input : 30 The largest is 30. The lowest is 10. Example 2: Input : 100 Input : 50 Input :...
In Java: Write a program that will count the number of characters, words, and lines in...
In Java: Write a program that will count the number of characters, words, and lines in a file. Words are separated by whitespace characters. The file name should be passed as a command-line argument, as shown below. c:\exercise>java Exercise12_13 Loan.java File loan.java has 1919 characters 210 words 71 lines c:\exercise> Class Name: Exercise12_13
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT