I'm trying to make a program that tries every permutation in the string to check if it can create palindrome string or not. If not it deletes one char and tries again and so on till you find a solution. I can't figure why it gives me a segmentation fault. Here is my code:
bool IsPalindrome(string s){
string t;
int x=s.size()-1;
for(int i=x;i>=0;i--)
t+=s[i];
if(s==t)
return true;
else
return false;
}
void generate_permutation(string s,int i){
sort(s.begin(),s.end());
do{
if(IsPalindrome(s)){
if(i%2==0){
cout<<"First"<<endl;
exit(0);
}
else{
cout<<"Second"<<endl;
exit(0);
}
}
}while(next_permutation(s.begin(),s.end())) ;
}
int main(){
string s;
cin>>s;
int i=0;
while(s.size()>=1){
generate_permutation(s,i);
s.erase(s.begin()+i);
i++;
}
}
int i=0;
while(s.size()>=1){ // size() goes down to 1
generate_permutation(s,i);
s.erase(s.begin()+i); // the i'th element is always the one erased
i++; // i goes up and up
Perhaps you intend to remove the first or last character, instead of the first, then the second, then the third…
Probably this is what you want
#include <bits/stdc++.h>
using namespace std;
int main() {
string s;
for (cin >> s; s.size(); ) {
sort(s.begin(), s.end());
do {
if (equal(s.begin(), s.end(), s.rbegin())) { // Palindrome check
cout << s << endl;
return 0;
}
} while (next_permutation(s.begin(), s.end())); // all permutations
string::iterator it = unique(s.begin(), s.end(), [](char a, char b) {
return a != b;
}); // first non repeating char
s.erase(it != s.end() ? it : s.begin()); // erase a char
}
return 0;
}
Sample Input
abaca
Sample Output
aaa
See demo http://ideone.com/VgTEgS.
Related
I have to write a programs that takes an input of string which has some '$' and digits. The output of the program is set of all possible strings where the '$ in the string is replaced by all the other digits.
I have written the following code for it.
#include<bits/stdc++.h>
using namespace std;
int numberOf(string in)
{
int count = 0;
for(int i = 0; i <= in.size()-1; i++)
if(in[i] == '$')
count++;
return count;
}
void solve(string in, string in1, vector <string> &s,
int index)
{
if(numberOf(in) == 0)
{
s.push_back(in);
return;
}
if(index == in.size())
{
return;
}
if(in1.empty())
{
return;
}
else
{
if(in[index] == '$')
{
string in2 = in;
in2[index] = in1[0];
string in3 = in1;
in3.erase(in3.begin());
solve(in2, in1, s, index+1);
solve(in, in3, s, index);
return;
}
else
{
solve(in, in1, s, index+1);
return;
}
}
}
void replaceDollar(string in)
{
string in1 = in;
int count = 0;
for(int i = 0; i <= in.size()- 1; i++)
{
if(in[i] != '$')
{
in1.push_back(in[i]);
count++;
}
}
count = in.size() - count;
cout << "Number is " << count << "\n";
vector <string> s;
solve(in, in1, s, 0);
for(auto i = s.begin(); i != s.end(); i++)
cout << *i << " ";
cout << "\n";
}
int main()
{
int t;
cin >> t;
while(t--)
{
string in;
cin >> in;
replaceDollar(in);
}
return 0;
}
For following input
1
$45
The expected output should be
445 545
But it returns
445 545 445 545
Can anyone please explain why is it outputting repeated strings?
Also can anyone suggest a better approach to this question?
Thanks in advance!
Assuming that this is homework:
Start over. Your code is way too complex for this problem.
I would treat everything as type char
Loop/iterate over said string, using std::string::replace() to replace each instance of $ with each digit.
-- If your teacher doesn't want you using std libraries, then add another loop and compare yourself.
3a. Of course, add a check, so that you don't replace $ with $
Create a new copy of the string on each iteration.
Print each to stdout as you create them.
See this post:
How to replace all occurrences of a character in string?
p.s. Pro tip: don't use using namespace. Use the full namespace in your calls; e.g.:
Bad
using namespace std;
string = "hello world";
Good
std::string = "hello world";
Hi I am trying to delete all the non-capitalized alphabet from a string input, but I am not quite sure where the error is in my coding. Please comment if you know why!
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
string CreateAcronym(string userPhrase) {
int i;
int stringSize;
char charAti;
stringSize = userPhrase.size();
for (i=0 ; i < stringSize ; i++ ) {
charAti = userPhrase.at(i);
if ( !isupper(charAti)) {
userPhrase.erase(i,1);
}
}
return userPhrase;
}
int main() {
string userSentence;
getline(cin , userSentence);
cout << CreateAcronym(userSentence) << endl;
return 0;
}
You cached old string length and continued to use while the string will become shorter by erasing characters.
You skip characters after characters to erase because i++ isn't canceled after erasure.
stringSize = userPhrase.size();
for (i=0 ; i < stringSize ; i++ ) {
charAti = userPhrase.at(i);
if ( !isupper(charAti)) {
userPhrase.erase(i,1);
}
}
should be
for (i=0 ; i < static_cast<int>(userPhrase.size()) ; ) {
charAti = userPhrase.at(i);
if ( isupper(charAti)) {
i++;
} else {
userPhrase.erase(i,1);
}
}
The problem have been answered by others, so I just add my "simpler" solution to the problem:
string CreateAcronym(string userPhrase) {
string result; // Create an empty string
// Loop over all the characters in the original string
for (char c : userPhrase) {
// If the character is upper-case...
if (isupper(c))
result += c; // Append it to the new string
}
return result; // Return the new string
}
You have 2 issues in your code.
First, you are erasing the string inside the loop (which changes its length), but using the precalculated length in the comparison.
Second, you only need to increment i when you don't erase a character. Otherwise, you will skip over some characters.
A working loop would be:
for (i = 0; i < userPhrase.size();) {
charAti = userPhrase.at(i);
if ( !isupper(charAti)) {
userPhrase.erase(i,1);
}
else {
++i;
}
}
You could simplify this loop by using an algoritm:
string CreateAcronym(string userPhrase) {
userPhrase.erase(std::remove_if(userPhrase.begin(),
userPhrase.end(), [](auto charAti) {
return !isupper(charAti); }),
userPhrase.end());
return userPhrase;
}
Here's a demo.
I am a beginner in solving algorithmic questions. Until now, I have only self-taught coding. So, I am not sure about the proper conventions.
I was trying to solve a question to reverse a string.There is some problem with the code but I am not sure what it is after debugging step-by-step.
class Solution {
public:
string reverseString(string s) {
int n = s.length();
string reverse;
for (int i=0;i<s.length();i++)
{
reverse[i] = s[n-1];
n=n-1;
}
return reverse;
}
};
Input: "Hello"
Output needed: "olleh"
My output: "olleh " (extra space)
Input: A man, a plan, a canal: Panama
Output: No output
I searched online for solutions. There were related to pointers. It would be great if someone helped me understand why this logic doesn't work and why using pointers is a better idea.
ALREADY GIVEN. CANNOT CHANGE:
string stringToString(string input) {
assert(input.length() >= 2);
string result;
for (int i = 1; i < input.length() -1; i++) {
char currentChar = input[i];
if (input[i] == '\\') {
char nextChar = input[i+1];
switch (nextChar) {
case '\"': result.push_back('\"'); break;
case '/' : result.push_back('/'); break;
case '\\': result.push_back('\\'); break;
case 'b' : result.push_back('\b'); break;
case 'f' : result.push_back('\f'); break;
case 'r' : result.push_back('\r'); break;
case 'n' : result.push_back('\n'); break;
case 't' : result.push_back('\t'); break;
default: break;
}
i++;
} else {
result.push_back(currentChar);
}
}
return result;
}
int main() {
string line;
while (getline(cin, line)) {
string s = stringToString(line);
string ret = Solution().reverseString(s);
string out = (ret);
cout << out << endl;
}
return 0;
}
As you create reverse, you have to pass the length of the string as an argument, else the created string will be of size 0. This could look like this:
string reverseString(string s) {
int n = s.length();
string reverse(n,'0');
for (int i=0;i<s.length();i++)
{
reverse[i] = s[n-1];
n=n-1;
}
return reverse;
}
Reversing a string is trivial. Just construct a new one from the reverse iterators:
std::string reverse_str(s.rbegin(), s.rend());
or
std::string reverse_str(s.crbegin(), s.crend());
Here's how I would write your function:
string reverseString(const string& s) {
return {s.crbegin(), s.crend()};
}
Try this out
class Solution {
public:
string reverseString(string s) {
//cout<<"inside func";
int n = s.length();
cout<<n<<endl;
char reverse[sizeof(char)*n];// reverse stores the reverse of original string s
int i= 0;
for ( i=0;i<s.length();i++)
{
reverse[i] = s[n-i-1];
}
return reverse;
}
}
int main()
{
string s,r;
Solution sol;
s= "hello";
r= sol.reverseString(s);
cout<<r<<endl;
cout<<r.length()<<endl;
return 0;
}
when i= 0, n-i-1= n-1 which is the last element of the original string s. So the first element of the reverse string is the last element of s. Next i becomes i+1 i.e 1. This time second element of the reverse string is the last but one element in string s. This procedure is repeated till i < s.length(). The element to get copied is for index i= n-1 and n becomes n-(n-1)-1= 0, so the last element of reverse string is the first element of s. After this the loop exists. No additional extra characters are added.
I've attempted to write a code that checks whether or not a string is a palindrome. Here is the code:
#include <iostream>
#include <string>
using namespace std;
bool pal(string str)//This block of code checks if input string is a palindrome
{
bool valid;
int i;
for (i = 0; i < str.length(); i++)
{
if (str[-i] == str[i])
{
valid = true;
}
else
{
valid = false;
}
}
return valid;
}
int main()
{
string s;
cin >> s;
if (!pal(s))
{
cout << "NO" << endl;
}
else
{
cout << "YES" << endl;
}
return 0;
}
Currently I am getting "Debug Assertion Fail" error.
str[-i] == str[i]
is a problem since negative indices are not valid indices in C++.
You need to change the strategy a little bit.
bool pal(string str)
{
int i = 0;
int j = str.length() - 1;
for ( ; i < j; ++i, --j)
{
if (str[i] != str[j])
{
// No need for any more checks.
return false;
}
}
// If we come here, the string is a palindrome.
return true;
}
C++ Provides us with an inbuilt function reverse() which can be used to reverse the Input string and compare it with un reversed string and print the output. The code goes as follows.
#include<iostream>
#include<algorithm>
using namespace std;
int main(){
string str;
cin>> str;
string rev;
rev = str;
reverse(str.begin(), str.end()); // string reverse operation
if(rev == str){
cout<<"YES"<<endl; // Prints "Yes" if string is palindrome
}else{
cout<<"NO"<<endl; // Prints "No" if string is not palindrome
}
return 0;
}
I am writing a program that will input an alphabetic message, and use the class to build the morse code string, and then output the string. I have written all the method and the program compiles fine. However when I enter the temp string and hit return. I am given a segmentation fault. I can't seem to find the problem. If anyone can see what the problem is I would greatly appreciate your help. Thanks.
#include <string>
#include <vector>
#include <fstream>
using namespace std;
class Code
{
public:
Code(); // Default constructor - loads and uses morse code
string decode(vector< string> message); // decodes a message
string encode(vector<char> message); // encodes a message
private:
vector<string> codewords; // this is a codeword vector parallel to A-Z
vector<char> alpha; // this is the vector for A-Z
vector<char> alphacode(); // function builds the vector alpha - A B C etc.
vector<string> morsecode(); // function builds the vector codewords containing morse code
char decode(string c); //returns the character for the codeword c.
string encode(char c);
};
Code::Code() {
alpha = alphacode();
codewords = morsecode();
}
string Code::decode(vector< string> message) {
string temp;
for (int i=0; i < message.size(); i++) {
temp += decode(message[i]);
}
return temp;
}
string Code::encode(vector<char> message)
{
string temp;
for (int i=0; i<message.size(); i++)
{
temp+=encode(message[i]);
}
return temp;
}
vector<string> Code::morsecode()
{ // This function returns a vector containing the morse code
vector<string> temp(28);
temp[0] =".-";
temp[1] ="-...";
temp[2] ="-.-.";
temp[3] ="-..";
temp[4] =".";
temp[5] ="..-.";
temp[6] ="--.";
temp[7] ="....";
temp[8] ="..";
temp[9] =".---";
temp[10] ="-.-";
temp[11] =".-..";
temp[12] ="--";
temp[13] ="-.";
temp[14] ="---";
temp[15] =".--.";
temp[16] ="--.--";
temp[17] =".-.";
temp[18] ="...";
temp[19] ="-";
temp[20] ="..-";
temp[21] ="...-";
temp[22] =".--";
temp[23] ="-..-";
temp[24] ="-.--";
temp[25] ="--..";
temp[26] =".......";
temp[27] ="x";
return temp;
}
vector<char> Code::alphacode()
{// This returns a vector containing the alphabet a-z and " "
vector<char> temp;
for (char c='A'; c<='Z'; c++)
temp.push_back(c);
temp.push_back(' ');
temp.push_back('.');
return temp;
}
char Code::decode(string c)
{
for (int i = 0; i < alpha.size(); i++) {
if(c == codewords[i]) {
return alpha[i];
}
}
}
string Code::encode(char c)
{
for (int i=0;i<codewords.size();i++)
{
if (c==alpha[i])
{
return codewords[i];
}
}
}
int main()
{
vector<char> message;
string temp;
getline(cin, temp);
for (int i=0; i <temp.length(); i++)
{
message.push_back(temp[i]);
}
Code C;
cout << C.encode(message) << endl;
}
Your alphacodes and morsecodes are only for Capital letters, so this function returns null resulting in problems.
string Code::encode(char c)
{
for (int i=0;i<codewords.size();i++)
{
if (c==alpha[i])
{
return codewords[i];
}
}
}
Your check for c==alpha[i] should either check ignoring the case or your alpha codes should have small alphabet codes as well. Your morsecodes should have the codes for small alphabets as well and your checks where you map A-Z to 0-28 should accommodate small letters.
Remember, small letters have different ASCII codes than Capital letters.
The following function does not have a return statement at the end.
string Code::encode(char c)
{
for (int i=0;i<codewords.size();i++)
{
if (c==alpha[i])
{
return codewords[i];
}
}
// What should happen if execution gets to this line?
}
If, per chance, your code reaches the end of the function, you will run into undefined behavior. That could be the source of your problem.