0

I have a project and I want to ask for some things. How can I make a string with descending order using cin<<word;? I tried this from a website that I found but it doesn't work with cin. Here is the code:

void descOrder(string s) 
{ 
    sort(s.begin(), s.end(), greater<char>()); 
} 

int main() 
{ 
    string s = "geeksforgeeks"; 
    descOrder(s); // function call 
    return 0; 
} 

To be more clear I want to do this

  • Input: geek for geeks
  • Output ssrokkggfeeee

Also, how can I replace letters from a string using the alphabet, for instance, the Hello I want to be like this H to be I, e to be f, l to be m, o to be p, and if a word contains the letter z I want to replace with the letter a.

The final question I want to print from a string first the according and after the vowels

asmmo
  • 6,922
  • 1
  • 11
  • 25
MikeK
  • 31
  • 4

2 Answers2

2

You are passing your std::string by value, hence desOrder() gets a copy from it and then sorts it and you get nothing.

Pass your std::string by reference to be able to change it not a copy from it.

#include <string>
#include <iostream>
#include <algorithm>

void descOrder(std::string & s)
{
    std::sort(s.begin(), s.end(), std::greater<char>());
}

int main()
{
    std::string s = "geeksforgeeks";
    descOrder(s); // function call
    std::cout << s;
    return 0;
}

Plz, post one question per post and see this Why is "using namespace std;" considered bad practice?

asmmo
  • 6,922
  • 1
  • 11
  • 25
  • Another solution would be to return the sorted string (by value) from descOrder: std::string descOrder(std::string s) { std::sort(s.begin(), s.end(), std::greater()); return s; } .. in main: int main() { auto s = descOrder("geeksforgeeks"); std::cout << s; return 0; } - this way you have a function with a clean return. – Wolfgang Jun 10 '20 at 10:28
  • @Wolfgang I want to correct something misunderstood by the OP and didn't want to change the OP's structure. – asmmo Jun 10 '20 at 10:31
  • 1
    I understand and welcome this, which is why I had inserted my comment only as a comment and not as an alternative answer. Nevertheless, I hoped to show the OP that there are other reasonable approaches. – Wolfgang Jun 10 '20 at 10:34
1

There are two methods applicable:

1. Pass value by reference:

When you pass a variable by reference, it does manipulates the original variable's value:

#include <iostream>
#include <algorithm>

void descOrder(std::string & s) // just use an '&' sign here
{ 
    sort(s.begin(), s.end(), std::greater<char>()); 
} 

int main() 
{ 
    std::string s = "geeksforgeeks";

    descOrder(s); // function call

    std::cout << s << std::endl;

    return 0; 
}

2. Return value instead of passing by reference:

If you don't want to change the original value but want to store in another variable or directly print it, you may do something like:

#include <iostream>
#include <algorithm>

std::string descOrder(std::string s) 
{ 
    sort(s.begin(), s.end(), std::greater<char>());
    return s;
} 

int main() 
{ 
    std::string s = "geeksforgeeks";

    std::string changedS = descOrder(s); // function call AND assigning to another variable

    std::cout << changedS << std::endl;

    // alternatively (uncomment)...
    // std::cout << descOrder(s) << std::endl; // if you just want to print

    return 0; 
}
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34