Reputation: 97
#include <iostream>
const int SIZE = 100;
using namespace std;
int main()
{
char *pStr, str[SIZE] = "", newStr[SIZE] = "", ch;
int count = 0, i = 0, j = 0;
cout << "Enter a number of mixed characters: ";
cin.getline(str, SIZE);
pStr = str;
while (*pStr != '\0')
{
if (isalnum(*pStr))
ch = toupper(*pStr);
newStr[i++] = ch;
if (*pStr = ' ')
count++;
pStr++;
}
newStr[i] = '\0';
cout << strlen(str) - strlen(newStr) << " characters were filtered out, "
<< " out of which " << count << " whitespaces were encountered.\n";
int temp;
for (i = 0; i < strlen(newStr) - 1; i++);
{
for (j = i + 1; j < strlen(newStr); j++);
{
if (newStr[j] < newStr[i]) // sorts in alphabetical
{ // and numerical order
temp = newStr[i];
newStr[i] = newStr[j];
newStr[j] = temp;
}
}
}
cout << "New sorted string: " << newStr << endl;
return 0;
}
I have a code here which is supposed to take an input string and print it out in a certain order and remove other characters as well as spaces. Numbers and letters are supposed to be sorted in numerical and alphabetical order. So if you take for instance the input "khff &%/321", the output should be "123FFHK".
However, when I try the code with said input string, the output I get is "KHFFFFFF32". I was hoping for some tips on what parts of the code I need to take a closer look at to solve the problem.
Upvotes: 0
Views: 537
Reputation: 11012
Here is another take on how the program could be written to use the standard library:
#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <cstdio>
#define USER_TYPES_INPUT // comment this line out to use own test stream
#ifdef USER_TYPES_INPUT
auto& my_cin = std::cin;
#else
std::stringstream my_cin{R"(khff&%/32 1)"};
#endif
int main()
{
std::string line;
std::cout << "Enter a number of mixed characters: \n";
std::getline(my_cin, line);
auto original_size = line.size();
auto space_count = std::count_if(line.begin(), line.end(), [](auto c){return ::isblank(c); });
line.erase(std::remove_if(line.begin(), line.end(), [](char c){ return !::isalnum(c); }), line.end());
std::transform(line.begin(), line.end(), line.begin(), [](auto& c){return ::toupper(c); });
std::sort(line.begin(), line.end());
std::cout << original_size - line.size() << " characters were filtered out, out of which "
<< space_count << " whitespace" << (space_count == 1 ? " was" : "s were") << " encountered.\n";
std::cout << "New sorted string: " << line << '\n';
return 0;
}
Upvotes: 1
Reputation: 6740
You can simply use this code to sort the string as you would like, then use the erase
function to strip out non-alphanumeric characters:
#include <iostream>
#include <algorithm>
#include <string>
int main() {
std::string word = "khff &%/123";
word.erase(std::remove_if(word.begin(), word.end(), [](char ch){ return !::isalnum(ch); }), word.end());
std::sort(word.begin(), word.end());
std::cout << word << '\n';
return 0;
}
Upvotes: 3
Reputation: 81
I would also like to point out
if (isalnum(*pStr))
ch = toupper(*pStr);
newStr[i++] = ch;
the list line in this is not covered under the if condition,and for every special character read(&,%,/) you would be appending them to your newStr,hence you are getting additional Fs in your output.You must do something like:
if (isalnum(*pStr))
{ ch = toupper(*pStr);
newStr[i++] = ch;
}
which will check whether your character is alnum or not and only append when if condition is satisfied.
Upvotes: 1