dimanche 15 mars 2020

How do I sort capital letters as well, I am able to sort the lowercase letter in C++

I have written the program which intends to take an input and return the output in ascending order of characters.

Example:

Scenario 1: Given an input as "Hello Rohit", the method should return "ehHillooRt"

Scenario 2: Given an input as "Hello Rohit!", the method should return an error message ("Invalid character ! at position 12") specifying first occurrence and the position of an invalid character.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <bits/stdc++.h>
using namespace std;


int main ()
{
  string str;
  int i, count, n;
  cout << "Enter a sentence : ";
  getline (cin, str);
  for (i = 0; str[i] != '\0'; i++) 
{
   if (str[i] == ' ')
   count++;
}
string strWords[count + 1];
short counter = 0;
for (short i = 0; i < str.length (); i++)
{
    if (str[i] == ' ')
 {
  counter++;
  i++;
 }
   strWords[counter] += str[i];
}
for (i = 0; i < strWords[i].length (); i++)
{
 string temp = strWords[i];
 sort (temp.begin (), temp.end ());
 cout << temp << " ";
}
return 0;
}

Please help!

Aucun commentaire:

Enregistrer un commentaire