How to remove consonants from a string? [closed] - c++

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Here is my code:
#include <iostream>
#include <string>
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
while(n--)
{
string str;
char a[] = {'a','e','i','o','u','A','E','I','O','U'};
getline(cin, str);
for(int i=0 ;i<str.length(); i++)
{
for(int j=0; j<10; j++)
{
if(str[i]==a[j])
{
cout << str[i];
}
}
}
cout << "\n";
}
return 0;
}
Test cases are :
HmlMqPhBfaVokhR
wdTSFuI
IvfHOSNv
I am not removing anything but I am printing only vowels. But, some test cases didn't pass. Maybe this code doesn't work on multiple test cases.

Try this for proper console in :
int main()
{
int n;
std::cin >> n;
std::cin.ignore(); // fix
/* remaining code */
return 0;
}
> To find the vowels in a string
On way of finding the vowels in a string is using a std::binary_search each character of the given string in a vowel table.
Make a sorted array of char s of all vowels(i.e. vowels array).
For each char of the input string, std::binary_search in the
vowels array.
If std::binary_search returns true(meaning the char is an vowel), print the char of the string.
Following is the example code! (See live online)
#include <iostream>
#include <string>
#include <algorithm> // std::for_each, std::binary_search, std::sort
#include <array> // std::array
int main()
{
std::array<char, 10> a{ 'a','e','i','o','u','A','E','I','O','U' };
std::sort(a.begin(), a.end()); // need sorted array for std::binary_search
const std::string str{ "HmlMqPhBfaVokhR wdTSFuI IvfHOSNv" };
std::for_each(str.cbegin(), str.cend(), [&](const char str_char)
{
if (std::binary_search(a.cbegin(), a.cend(), str_char))
std::cout << str_char << " ";
});
return 0;
}
Output:
a o u I I O
> To remove the vowels from a string
Use erase-remove idiom as follows(till c++17†).
Make a sorted array of char s of all vowels(i.e. vowels array).
Using std::remove_if, collect the iterators pointing to the characters, which are vowels. A lambda function can be used as the predicate for std::remove_if, where the std::binary_search is used to check the char in the string exists in the vowels array.
Using std::string::erase, erase all the collected characters(i.e. vowels) from the string.
Following is an example code! (See live online)
#include <iostream>
#include <string>
#include <algorithm> // std::sort, std::binary_search, std::remove_if
#include <array> // std::array
int main()
{
std::array<char, 10> a{ 'a','e','i','o','u','A','E','I','O','U' };
std::sort(a.begin(), a.end()); // need sorted array for std::binary_search
std::string str{ "Hello World" };
// lambda(predicate) to check the `char` in the string exist in vowels array
const auto predicate = [&a](const char str_char) -> bool {
return std::binary_search(a.cbegin(), a.cend(), str_char);
};
// collect the vowels
const auto vowelsToRemove = std::remove_if(str.begin(), str.end(), predicate);
// erase the collected vowels using std::string::erase
str.erase(vowelsToRemove, str.end());
std::cout << str << "\n";
return 0;
}
Output:
Hll Wrld
† Since c++20, one can use std::erase_if for this, which would be less error prone than the the above one. (See online live using GCC 9.2)
#include <iostream>
#include <string> // std::string, std::erase_if
#include <array> // std::array
int main()
{
std::array<char, 10> a{ 'a','e','i','o','u','A','E','I','O','U' };
std::sort(a.begin(), a.end()); // need sorted array for std::binary_search
std::string str{ "Hello World" };
// lambda(predicate) to check the `char` in the string exist in vowels array
const auto predicate = [&a](const char str_char) -> bool {
return std::binary_search(a.cbegin(), a.cend(), str_char);
};
std::erase_if(str, predicate); // simply erase
std::cout << str << "\n";
return 0;
}
> To remove the consonants from a string
To remove the consonants from the given string, in the above predicate negate the result of std::binary_search. (See live online)
const auto predicate = [&a](const char str_char) -> bool {
return !std::binary_search(a.cbegin(), a.cend(), str_char);
// ^^ --> negate the return
};
As side notes,
Avoid the #include<bits/stdc++.h> Read more: Why should I not #include <bits/stdc++.h>?
Do not practice with using namespace std; Read more: Why is "using namespace std;" considered bad practice?

Apart from the std::getline problem already answered:
for(int i=0 ;i<str.length(); i++)
{
for(int j=0; j<10; j++)
{
if(str[i] == a[j])
{
// this is the one you do NOT want to print...
// cout<<str[i];
// skip instead:
goto SKIP;
}
}
std::cout << str[i]; // output the one NOT skipped...
SKIP: (void)0;
}
OK, don't want to start any discussion about usage of goto, there are many ways to avoid it, e. g. by packing the inner for loop into a separate (inline) function. You can have it easier, though, as there already exists such a function; code gets even easier with a range-based for loop:
for(auto c : str)
{
if(!strchr("aeiouAEIOU", c))
{
std::cout << c;
}
}
strchr (from cstring) returns a pointer to the first character in the string equal to the reference character - or nullptr if not found...
To really remove the vowels from the string in a modern C++ way, consider this:
str.erase(std::remove_if(
str.begin(), str.end(),
[](char c) { return strchr("aeiouAEIOU", c) != nullptr; }
), str.end());

Your code probably should looks like (please see comments inline):
#include <iostream>
#include <string>
using namespace std;
int main() {
string vowels = "aeiouAEIOU";
int n;
cin>>n; // assume this stands for line count
while(n-- >= 0)
{
string str, result;
getline(cin, str);
for(int i=0 ;i<str.length(); i++)
{
if (vowels.find(str[i]) != std::string::npos)
result += str[i]; // add character to result if it is not consonant
}
cout<<result<<"\n"; // print result
}
return 0;
}

Related

How to capitalize the first letter of each name in an array?

Here is the question:
Create a function that takes an array of names and returns an array where only the first letter of each name is capitalized.
example
capMe(["mavis", "senaida", "letty"]) ➞ ["Mavis", "Senaida", "Letty"]
And the code I wrote to answer this question:
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void capme(vector<string> name)
{
char ch;
for(int i = 0; i < name[i].size(); i++)
{
putchar(toupper(name[i][0]));
cout << name[i] << endl;
}
}
int main()
{
vector <string> name = {"mavis", "senaida", "letty"};
capme(name);
return 0;
}
As you can see, it prints "Mmavis", "Ssenaida", "Lletty", which is wrong. Can you guys help me in answering this question as I don't know how?
To change the input argument, we have two choice: make the argument mutable reference, or add a return type, here I choose the first one.
putchar can be used to print only one character, it recommended to use cout to print a string, possible solutions:
with traditional loop: capme
with range for-loop since c++11 : capme2
with stl algorithm transform: capme3
Don't forget to check if the string element is empty, or you may crash while accessing the first character.
To obey the single-responsibility principle (SRP), it's better to print the string vector out of the capme function.
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void capme(vector<string>& name) {
for (int i = 0; i < name[i].size(); i++) {
if (name[i].empty()) continue;
name[i][0] = toupper(name[i][0]);
}
}
void capme2(vector<string>& names) {
for (auto& name : names) {
if (name.empty()) continue;
name[0] = toupper(name[0]);
}
}
void capme3(vector<string>& names) {
std::transform(names.begin(), names.end(), names.begin(), [](auto& s) {
return s.empty() ? s : (s[0] = toupper(s[0]), s);
});
}
Online demo
You have used the wrong function. What you need is a replacement and not a prepend. Try using std::string::operator[] to access the first element of the words in the vector. This is how I would write this code:
std::vector<string> capitaliseInitLetter(std::vector<string> vec) {
for (auto& word : vec) {
word[0] -= 32; //add a check to see if the letter is already capital
}
return vec;
}
The above code is just an example which assumes that the input is valid. You'll have to add checks and exception handling for invalid inputs on your own. (Hint: Take a look at #prehistoricpenguin's answer)
You are calling putchar() which writes a character to standard output, and in this case is printing the first letter of each string in name as uppercase, then you are writing the entire string to standard output immediately after.
Additionally, your function does not meet the requirements you stated above saying it should return an array where the strings have the first letter capitalized.
What you could do is change the signature of capme() to return a std::vector<std::string>, and perhaps utilize the for_each() function to handle changing the first letter of each string in your vector then return it.
For reference:
#include <vector>
#include <string>
#include <algorithm>
#include <cctype>
std::vector<std::string> capme(std::vector<std::string> name)
{
std::for_each(name.begin(), name.end(), [](std::string &s) {
s[0] = toupper(s[0]);
});
return name;
}
Or as kesarling suggested, a simple for each loop:
std::vector<std::string> capme(std::vector<std::string> name)
{
for (auto& s : name) {
s[0] = toupper(s[0]);
}
return name;
}

How do I print the frequency of each word in the given input string using two vectors?

Here is my approach where I have tried to split the string into words and then move forward but this is not working.
For instance, the input is: hey hi Mark hi mark
Then the output should be:
hey-1
hi-2
Mark-1
hi-2
mark-1
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
vector<vector<string> > strs;
string str;
cout<<"Enter your strings"<<endl;
getline(cin, str);
int len=str.length();
int j=0;
string s="";
for(int i=0; i<len; i++){
s+=str[i];
if(str[i+1]==' ' || i+1==len){
strs[0][j]=s;
s="";
j++;
i++;
}
}
strs[0][j]="NULL";
int freq;
vector<int> frequency;
for(int n=0; strs[0][n]!="NULL" ;n++){
freq=1;
for(int m=0; strs[0][m]!="NULL"; m++){
if(strs[0][n]==strs[0][m]){
freq++;
}
frequency.push_back(freq);
}
}
for(int x=0; strs[0][x]!="NULL"; x++){
cout<<strs[0][x]<<" - "<<frequency[x]<<endl;
}
return 0;
}
In your code, you have tried to access string elements via its index, which sometimes raises segmentation fault. To solve your problem, I came up with below mention solution.
#include <iostream>
#include <string>
#include <map>
/* getWordFrequency : function with return type std::map<std::string, int>
Param1: Input string
Param2: Default delimiter as " "(void space).
*/
std::map<std::string, int> getWordFrequency(const char *input_string, char c = ' ')
{
// Container to store output result
std::map<std::string, int> result;
// Iteration loop
do{
// Iteration pointer to iterate Character by Character
const char *begin = input_string;
// Continue loop until delimeter or pointer to self detects
while(*input_string != c && *input_string){
// Jump to next character
input_string++;
}
// Iterator for output result container
std::map<std::string, int>::iterator finder = result.find(std::string(begin, input_string));
// Find element using iterator
if(finder != result.end()){
// Element already present in resultunt map then increment frequency by one
finder->second += 1;
} else {
// If no element found then insert new word with frequency 1
result.insert(std::pair<std::string, int>(std::string(begin, input_string),1));
}
} while (0 != *input_string++); // Continue till end of string
return result;
}
int main()
{
// Your string
std::string input_string = "hey hi Mark hi mark";
// Container to catch result
std::map<std::string, int> frequency = getWordFrequency(input_string.c_str());
// Printing frequency of each word present in string
for (auto element : frequency){
std::cout << element.first << "-" << element.second << std::endl;
}
return 0;
}
So, I think your approach using 2 std::vectors is unfortunately wrong. You do not fully understand the difference between char and std::string.
You need to learn abaout that.
There is a more or less standard approach for counting something in a container, like a string or in general.
We can use an associative container like a std::map or a std::unordered_map. And here we associate a "key", in this case the "word" to count, with a value, in this case the count of the specific word.
And luckily the maps have a very nice index operator[]. This will look for the given key and, if found, return a reference to the value. If not found, then it will create a new entry with the key and return a reference to the new entry. So, in both cases, we will get a reference to the value used for counting. And then we can simply write:
std::map<std::string, int> counter{};
counter[word]++;
And that's it. More is not necessary. Please see:
#include <iostream>
#include <string>
#include <sstream>
#include <unordered_map>
int main() {
// Our test String
std::string text{"hey hi Mark hi mark"};
// Here, we will store the result of the counting
std::unordered_map<std::string, unsigned int> counter;
// Now count all words. This one line does all the counting
for (std::istringstream iss{text}; iss >> text; counter[text]++);
// Show result to user
for (const auto& [word, count] : counter) std::cout << word << '-' << count << ' ';
}
It seems also that splitting a string is some how difficult for you. Also here are many many ppossible solutions available.
One of the more sophisticated and more advanced solution is to use the std::sregex_token_iterator. With that you can easily iterate over patterns (described by a std::regex) in a string.
The final code will look nearly the same, but the result will be better, since for example punctuation can be excluded.
Example:
#include <iostream>
#include <string>
#include <unordered_map>
#include <regex>
#include <iterator>
using Iter = std::sregex_token_iterator;
const std::regex re{R"(\w+)"};
int main() {
// Our test String
std::string text{"hey hi Mark, hi mark."};
// Here, we will store the result of the counting
std::unordered_map<std::string, unsigned int> counter;
// Now count all words. This one line does all the counting
for (Iter word(text.begin(), text.end(), re); word != Iter(); counter[*word++]++);
// Show result to user
for (const auto& [word, count] : counter) std::cout << word << '-' << count << ' ';
}

How to make a 2d vector from input string?

I got this problem in one of the coding questions in my university exam where I knew the answer but couldn't follow as I didn't know how to parse the string and convert it into a 2d vector.
So I have got a 2d vector in the form of a string
"[[1,4], [5,7], [4,1], [8,9]]"
I want to convert this string into a vector<vector<int>>
EDIT
Maybe I wasn't clear the last time and there's one thing I missed. The string is
"[[1,4], [5,7], [4,1], [8,9]]"
I want this string to be in the form of a 2d vector. So, let's say I have a vector vec defined as vector<vector<int>> vec. Then vec[0]={1,4}, vec[1]={5,7}, vec[2]={4,1}, vec[3]={8,9}. Below is what I did and it shows incorrect output
#include<iostream>
#include<sstream>
#include<vector>
using namespace std;
int main()
{
string str="[[1,4], [5,7], [4,1], [8,9]]";
stringstream ss(str);
vector<vector<int>> vec;
string temp;
while(ss>>temp){
cout<<temp<<endl;
vector<int> t;
int x,y;
stringstream ss2(temp);
while(ss2>>x>>y)
{
t.push_back(x);
t.push_back(y);
}
vec.push_back(t);
}
}
The generic answer is to write a recursive descent parser (look it up). It's a fancy way of saying that you write a function for each of the non-terminals (vector2, vector, int) then you usually look at just the first byte to figure out to do. In this case your grammar might be:
vector2 = "[" vector (, " " vector) "]"
vector = "[" int (, number) "]"
number = 0 | 1 [0-9]
Then you implement number, vector and vector_vector similar to how #StPiere showed you previously.
I usually use c, and I found this generic function signature to be a useful start:
char *parse_something(const char *s, something *v)
where s is the string you are parsing and the return value is what the next thing is you want to parse.
Here is quick implementation Godbolt:
Note: This answer does not handle cases where integers have more than one digit. It is not generic or easily extensible, but as this is a homework question, you can understand and improve the code.
#include <string>
#include <vector>
#include <istream>
#include <sstream>
#include <cctype>
#include <iostream>
#include <iterator>
int main()
{
std::string input{"[[1,4], [5,7], [4,1], [8,9]]"};
std::stringstream ss{input};
auto it = std::istream_iterator<char>{ss};
std::vector<std::vector<int>> output;
std::vector<int> inner;
// Loop through the string, one character at a time
while (it != std::istream_iterator<char>{})
{
++it;
// If we encounter a digit, push it into the inner vector
if (std::isdigit(*it))
{
// Dirty hack to convert char to integer (assumes ASCII)
inner.push_back(*it - 48);
}
// If you encounter a ']' char, then push inner vector into the outer vector
// and then clear the inner vector
// For the final ']', the inner vector will be size 0, ignore this case
if (*it == ']')
{
if (inner.size() != 0)
{
output.push_back(inner);
}
inner.clear();
}
}
}
#include <iostream>
#include <vector>
using namespace std;
template <typename Range>
void print_range(const Range& r)
{
for (const auto& e : r)
cout << e << " ";
cout << endl;
}
int main()
{
vector<int> v;
vector<vector<int>> vv;
char c;
int cnt = 0;
while (cin >> c)
{
if (c == ',' || c == ' ' || c == '\n')
{
continue;
}
if (c == '[')
{
++cnt;
v.clear();
continue;
}
if (c == ']')
{
--cnt;
if (cnt == 0)
break;
vv.push_back(v);
v.clear();
continue;
}
cin.unget();
int x;
if (cin >> x)
v.push_back(x);
}
for (const auto& vec : vv)
print_range(vec);
}

How to apply tolower to a string vector?

Since tolower only works for individual characters, how would I use tolower for a string vector? I know I would have to go through each individual character of each item, however I am unsure how to access the individual characters within each item.
eg:-
string vector[Apple, Banana].
vector[0] is Apple but I want the character not the whole string. Thanks in advance!
#include<iostream>
#include<vector>
#include<string>
using namespace std;
int main() {
vector<string> fruits;
fruits.push_back("Apple");
fruits.push_back("Banana");
for(int i = 0; i<fruits.size(); i++){
for(auto& c : fruits[i])
{
c = tolower(c);
}
cout << fruits[i] << endl;
}
}
Basically we loop through the character in the vector and make it tolower.
std::transform can help you here to apply tolower to each character in each string inside in your vector:
#include <iostream>
#include <algorithm> // std::transform
#include <string>
#include <vector>
int main()
{
//Your vector of strings
std::vector<std::string> fruits = { "Apple", "Banana" };
//Loop through vector
for (auto &fruit : fruits)
{
//Apply tolower to each character of string
std::transform(fruit.begin(), fruit.end(), fruit.begin(),
[](unsigned char c) { return std::tolower(c); });
}
for (auto const& fruit : fruits)
std::cout << fruit << ' ';
}
Demo
As #AlanBirtles suggested you could apply std::transform for the outer loop too. Then it becomes like this:
std::transform(fruits.begin(), fruits.end(), fruits.begin(),
[](std::string &fruit)
{
std::transform(fruit.begin(), fruit.end(), fruit.begin(),
[](unsigned char c) { return std::tolower(c); });
return fruit;
});
Demo

How to check if a string ends with 'ed' in C++;

How to write a program that reads 5 strings from user input and prints only those strings that end with the letter ‘ed’ in C++. Need help!
The solution is rather straightforward.
First we define a container that can contain 5 std::string. For that we use a std::vector together with a constructor to reserve space for the 5 elements.
Then we copy 5 strings from the console (from user input) into the vector.
And, last, we copy elements out of the std::vector to std::cout, if the strings end with "ed".
Because of the simplicity of the program, I cannot explain much more . . .
Please see.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
constexpr size_t NumberOfTexts = 5U;
int main()
{
// Define a container that can hold 5 strings
std::vector<std::string> text(NumberOfTexts);
// Read 5 strings from user
std::copy_n(std::istream_iterator<std::string>(std::cin), NumberOfTexts, text.begin());
// Print the strings with ending "ed" to display
std::copy_if(text.begin(), text.end(), std::ostream_iterator<std::string>(std::cout,"\n"), [](const std::string& s){
return s.size()>=2 && s.substr(s.size()-2) == "ed";
});
return 0;
}
Simple solution,
#include<iostream>
using namespace std;
bool endsWith(const std::string &mainStr, const std::string &toMatch)
{
if(mainStr.size() >= toMatch.size() &&
mainStr.compare(mainStr.size() - toMatch.size(), toMatch.size(), toMatch) == 0)
return true;
else
return false;
}
int main()
{
string s[5];
for(int i=0;i<5;i++)
{
cin>>s[i];
}
for(int i=0;i<5;i++)
{
if(endsWith(s[i],"ed"))
cout<<s[i]<<endl;
}
}
Hope This might Helps:)