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...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to...
C++ Question- Write function templates for 5 sort algorithms: - Quick Sort Apply these algorithms to 10 different arrays of integers. Each array has 100 integer elements. The arrays are filled with random integer numbers.
IN PYTHON Write a function capitalize_last(phrase) that takes in a string phrase consisting of arbitrarily capitalized...
IN PYTHON Write a function capitalize_last(phrase) that takes in a string phrase consisting of arbitrarily capitalized words separated by spaces, and returns a new string consisting of those words but with the only the last letter in each word uppercase. You can assume that the string contains only letters and spaces, no punctuation. Examples: >>> capitalize_last('tEst WiTH rANdoM capITaliZATioN') 'tesT witH randoM capitalizatioN' >>> capitalize_last('') '' >>> capitalize_last('i am the senate') 'I aM thE senatE'
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.
A palindrome is a string of characters (a word, phrase, or sentence) that is the same...
A palindrome is a string of characters (a word, phrase, or sentence) that is the same regardless of whether you read it forward or backward – assuming that you ignore spaces, punctuation and case. For example, Race car is a palindrome. So is A man, a plan, a canal: Panama. 1. Describe how you could use a stack to test whether a string is a palindrome. 2. Describe how you could use a queue to test whether a string is...
C Write a function int sort(int** arr, int n) that takes as input an array of...
C Write a function int sort(int** arr, int n) that takes as input an array of int pointers, multiplies the ints the pointers point to by -1, and then sorts the array such that the values pointed to by the pointers are in decreasing order. For example, if the array is size 10 the pointer at index 0 will point to the largest value after multiplying by -1 and the pointer at index 9 will point to the smallest value...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT