How to apply mask to string in C/C++? [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I've been asigned with a homework, but I don't know what to do exactly.
Input file is randomly generated from letter from 'a' to 'z'. User will type a mask only with 0/1, for example 011011, where 1 means vowel and 0 means consonant.
Output will be all matches found in input that match the user-given mask (for example for 011011 output will be abbezz).
Any idea how to make this? I don't ask for code, but only for easiest way how to make this possible in c/c++.
Thanks

My understanding is that the requirements are to find all sequences of letters that match the mask.
Given the mask: 010 (consonant, vowel, consonant)
Here are the matches for a couple of words:
"are" - fail, first character is a vowel.
"mat" - pass, 'm' is a consonant, 'a' is a vowel, 't' is a consonant.
"mate" - fail, too many letters.
You will need to have some functions that test a letter for vowel or consonant.
Also consider using a state machine. If a test fails, you want to go back to the starting state.
Write down your algorithm first, step by step. Come up with a few test cases to verify your algorithm. After algorithm works, code it up. Use your same test cases for verifying the program.

Map the string to 0/1 according to the rules you have and search for substrings in the new string that match the mask.

I still do not know exactly what you are trying to accomplish, but maybe it helps to know that once you have your input in a std::string, you can iterate over the characters using iterators and any algorithm. Here is an example with std::for_each:
#include <algorithm>
#include <iostream>
#include <string>
void applyMask(std::string::reference aCharacter)
{
// apply your mask here, character by character
if ('a' == aCharacter) {
aCharacter = 'A';
}
}
int main()
{
std::string s("stackoverflow");
std::for_each(s.begin(), s.end(), &applyMask);
std::cout << s << std::endl;
}
You might want to have a look at std::transform, e.g. http://www.cplusplus.com/reference/algorithm/transform/

Related

Is there a limit for the IO buffers? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I want to let the users input strings with more than 4,095 characters, but std::cin/std::istream seems to have some sort of undocumented limit and keeps cutting off the string to only allow 4,095 characters.
Is there some way to get past this "limit" using one of the many input functions, preferably allowing any length? There might not be a limit in the cpp standard, but the operating system I am currently using may have an input buffer limit - Arch Linux.
The strings may reach lengths of 1 ≤ L ≤ 10e6 where L is the length of the string.
int main() {
/* The user inputs a string with more than
4095 characters.
Let's say that every character in the string
is 'a' except for the 4095th character in the
string, which is 'B', and the end of the string
looks like "...aaBaaaaaaaaaaaaa"
The string will get cut off to "...aaB" (4095
characters) meaning that there seems to be some
sort of limit. */
std::string str;
/* Manually paste a string that has 10,000
characters into a terminal emulator (Alacritty)
with CTRL+C → SHIFT+INSERT */
std::cin >> str;
/* This next line is supposed to output 10,000,
but instead, it outputs 4,095. */
std::cout << str.length() << std::endl;
}
The provided example should be simple enough to understand without needing a better reproducible example. Providing a better example would only clutter the post with a lot of unneccessary information since it should be simple enough as it is.
The input data I give it looks like this aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa but imagine 10,000 a's and it does not have any whitespaces.
Note: This program is compiled with this command:
g++ -g -O2 -std=gnu++17 -static light.cpp && ./a.out

How does this code works in c++? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I participated in Codeforces Round #396 (Div. 2) yesterday.
The (A) problem seemed pretty advanced in my opinion when I read it.
I tried solving it all the competition time yesterday and some time today.
I came up with a 200 lines long(half-working) solution. And then I gave up.
I looked what other people wrote there and I saw max 20 lines long code that seems magic to me.
The problem asks you to output the length of the longest uncommon subsequence of letters from two strings.
You can read the full problem here:
http://codeforces.com/contest/766/problem/A
#include<bits/stdc++.h>
using namespace std;
string a,b;
int main(){
cin>>a>>b;
printf("%d",a==b?-1:max(a.size(),b.size()));
return 0;
}
This is all the code used to solve the problem, and I really want to know how that one line of code
printf("%d",a==b?-1:max(a.size(),b.size()));
can solve this "advanced" task?
If the two strings are equal, there is no "uncommon subsequence". If they are not equal, neither one is a subsequence of the other, but each one is a subsequence of itself, so each one is an "uncommon subsequence". The longer of the two is the longest "uncommon subsequence", and its length is the correct answer. If the two are not equal but have the same length, then each one is an "uncommon subsequence" and the length of the longest is just the length of either one.
Don't get tangled up in what you (or any other reasonable person) think a "subsequence" is. The problem defines "uncommon subsequence", and all you have to do is apply its definition. This "problem" is about word play, not coding.
It does not seem to solve the task at hand. It just outputs -1 when the strings are equal and the length of the longer string when they are not different. However, the problem asks for the longest uncommon sequence. It would fail with the inputs abc and abcd to give the answer 1 would rather give 4.
What the line does is the following:
It checks whether a == b, whether the strings are equal.
If they are equal, the condition A is true in the ternary operator A ? B : C, so that expression will evaluate to B which is just -1 here.
If they are not equal, it will evaluate to C which is the maximum of the two string lengths, so it will be length of the longer string.
The value of the ternary expression is then printed, albeit without a newline, which is a bit bad.
So the code is so short because it only solves the problem for a few cases but not the general case.
printf ("%d", a == b ? -1 : max (a.size (), b.size ()));
is equivalent to
if (a == b) {
printf ("%d", -1);
}
else {
if (a.size () > b.size ()) {
printf ("%d", a.size ());
}
else {
printf ("%d", b.size());
}
}
BTW That's only a small step in solving the problem...
[CORRECTION]
IT DOES SOLVE THE WHOLE PROBLEM (See answer of Pete Becker)

The C++ equivalent of C's format string [closed]

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 6 years ago.
Improve this question
I have a C program that reads from keyboard, like this:
scanf("%*[ \t\n]\"%[^A-Za-z]%[^\"]\"", ps1, ps2);
For a better understanding of what this instruction does, let's split the format string as follows:
%*[ \t\n]\" => read all spaces, tabs and newlines ([ \t\n]) but not store them in any variable (hence the '*'), and will keep reading until encounter a double quote (\"), however the double quote is not input.
Once scanf() has found the double quote, reads all caracters that are not letters into ps1. This is accomplished with...
%[^A-Za-z] => input anything not an uppercase letter 'A' through 'Z' and lowercase letter 'a' through 'z'.
%[^\"]\" => read all remaining characters up to, but not including a double quote into ps2 ([^\"]) and the string must end with a double quote (\"), however the double quote is not input.
Can someone show me how to do the same thing in C++
Thank you
C++ supports the scanf function. There is no simple alternative, especially if you want to replicate the exact semantics of scanf() with all the quirks.
Note however that your code has several issues:
You do not pass the maximum number of characters to read into ps1 and ps2. Any sufficiently input sequence will cause a buffer overflow with dire consequences.
You could simplify the first format %*[ \t\n] with just a space in the format string. This would also allow for the case where no whitespace characters are present. As currently written, scanf() would fail and return 0 if no whitspace characters are present before the ".
Similarly, if no non letters or if no other characters follow before the second ", scanf would return a short count of 0 or 1 and leave one or both destination array in an indeterminate state.
For all these reasons, it would be much safer and predictable in C to first read a line of input with fgets() and use sscanf() or parse the line by hand.
In C++, you definitely want to use the std::regex package defined in <regex.h>.

Odd characters in output [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I'm trying to write a simple hangman game in c++ by randomly selecting a word from a list, checking the string length, and writing that many *s into a new string to serve as placeholders in the yet un-guessed word. The max length is 9 letters. I have the game working almost flawlessly -- the problem is that whenever my word has 8 or 9 letters, the program prints the correct number of *s followed by one or two � characters. Research tells me these are unprintable characters, but I've tried for a while now and I'm not sure why they're here, why they only show up with a word length>7, or how to get rid of them. Below is relevant code. Any suggestions?
Generating *s:
char word[80];
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word[i]='*';
}
You forgot to add the \0 terminator at the end of the string. After the for loop, add:
word[i] = '\0';
Or, best, use std::string instead of a C string.
Try using std::string instead.
std::string word;
int len=strlen(targetWord);
for(int i=0;i<len;i++){
word+='*';
}

what's number anagram which are palindromes in string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
What is the number of anagrams which are palindromes in a string?
Example : string = "aaabbbb";
Possible anagram's which are palindromes "abbabba" , "bbaaabb" and "bababab".
The problem here is the time, i have string of size 10^9.
here's my final code can anybody tell me what's the wrong with it ?
Every letter in your input string has to appear in an even amount, execpt one letter can appear in an odd amount. This letter has a fixed position in the palindron. It has to be exactly in the middle. Lets say the amounts of the letter a,b,c,... are #a, #b, #c, ...
You only care about half of those letters, because in an palindron, the second half depands of the first half. So we only use half of the letters:
I used the floor function, so I calculate the letter, which appears in an odd amount, correct.
So how many permutations are in the first half? This is a case of distinct permutation, so we get
possibilities.
For your example:
string = "aaabbbb";
We get: #a=3, #b=4. Therefore
We get 3 palindroms, these are "abbabba" , "bbaaabb" and "bababab", like you posted.
So, if you have a very large string:
Count the amounts of each letter
Check, if there is only 1 letter that appears in an odd amount. It there are more, you can't create palindroms.
Use the formular to calculate the number of different palindroms.
Since each side of the anagram must be a mirror image of the other, the number of anagrams we care about is basically just the number of anagrams we can form on one side, so:
group the characters in the string so identical characters are together (e.g., by sorting).
Check for an odd number of more than one character (of so, # anagrams = 0).
Take half the characters of each group of identical (truncating in the case of odd number).
Compute the number of unique permutations of those characters.