Finding matching letters in a string at same spots [closed] - c++

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I need a code to compare two strings: word and ptw. Both strings only have one word, and are of equal length and same case. The code needs to check the first character of both words and see if they are same and it will do this untill the end of the word. Then it will output the amount of matching letters (I don't need to know the matching letters).
Example: If word is windows and ptw is winowes it should output 4 as w, i, n, and s match.
I have tried the following however it does not check the positions:
string matchingletters(string word, string ptw) {
string result = "";
sort(begin(word), end(word));
sort(begin(ptw), end(ptw));
std::string intersection;
std::set_intersection(begin(word), end(word), begin(ptw), end(ptw),
back_inserter(intersection));
string mlr = to_string(intersection.length());
result = mlr + result;
cout << result << endl;
return result;
}
The result this gives when word is kanton and ptw is balkon is 4.
It counts k even though k is at 0 position in word and 3 position at ptw and thus they are not in the same postion and should not be counted.

Assuming the two words have the same length and since you don't care which letters match you can simply iterate and count matching characters
unsigned matchingletters(const std::string& word, const std::string& ptw) {
assert(word.size() == ptw.size());
unsigned count{0};
for (size_t i = 0; i < word.size(); ++i) {
if(word[i] == ptw[i])
count++;
}
return count;
}

Related

Given a string output it in a specific way using recursion? [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 2 months ago.
Improve this question
Given a string we have to output the string in a special way.
• If the string consists of one char, we output that char normally.
• Otherwise, we divide the string into two equal parts (if the number of letters in the substr is odd, the second part of the substr will be one letter longer than the first), and we output the first part twice and then the second part (according to the same rules).
For example, let's assume that we want to output the string YOGURT. We divide that string into two equal parts: YOG and URT.
How will we output the substr YOG? Again, it will be divided into two parts - Y and OG. The substr Y we output normally (but in the output of the substr YOG we will do it twice), and the substr OG we output as OOG. So the substr YOG we output as YYOOG.
Analogously, the substr URT is going to give the output UURRT. So the string YOGURT is going to be output as YYOOGYYOOGUURRT.
Length of the string can at max be 10000.
Now I tried using a non recursion way to solve this problem but it was way to slow so I have come to an conclusion I have to do this with recursion. And since I don't have that much experience with recursion I would really need some help.
This is very naturally implemented with recursion like so:
void print(std::string_view s) {
if (s.size() <= 1) std::cout << s;
else {
auto m = s.size() / 2;
print(s.substr(0, m));
print(s.substr(0, m));
print(s.substr(m));
}
}

Check an Array String character value without extra variable [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 1 year ago.
Improve this question
suppose I've this code:
string str[] = {"devil", "chandra"};
// check if str[0] has properly devil, character by character or not without extra variable
Now I want to check str[ 0 ]'s all character which is 'd','e','v','i','l' one by one without extra variable.
with extra variable code will be :
string n1 = "devil";
for(int i=0; i<1; i++){
string s1 = str[i]
for(int j=0; j<s1.size(); j++){
if(s1[i] == n[i]){
cout << s1[i] << " ";
}
}
Basically, I want O(n) loop where I can access all indexes string and among them all characters.
Like s[ i ] is "devil" and s[[i]] = 'd' something like this, Know it's not valid, but is there any way to do that??
Even I don't know is it a valid question or not!
I'm not sure why you would need an extra variable. If you need a conditional that checks that the first value in the array of strings is "devil", it shouldn't be anymore complicated than:
if (str[0] == "devil")
{
* Do things *
}
C++ can check a standard string all at once. You don't need to check each individual character if that's what you're thinking.
Keep in mind, this isn't going to account for situations where the string is not exactly the same. For instance, if str[0] has "Devil" instead of "devil", then the conditional will evaluate to false.

Can someone explain this program to check unique chars in string 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 7 years ago.
Improve this question
Hi I used to solve string char uniqueness using map in C++. I found this solution somewhere and working fine. But I can not understand how it is working. Please some one explain.
bool isUnique(string s){
int check = 0;
for(int i=0;i<s.length();++i){
if(s[i] != ' '){
int val = s[i]-'a';
if( (check & ( 1 << val)) > 0) return false;
check = check | (1 << val);
}
}
return true;
}
It returns true if string has no repeated character excluding spaces otherwise returns false.
It is using an int as if it were a bitmap. A bitmap is certainly a better data structure for a character uniqueness test than a map. An int is a crude and questionable (in this case) substitute for a bitmap.
Assume an int has 32 bits. Those bits are allocated in this code for the first 32 characters beginning with lower case 'a'. So the upper case letters and most special characters have no bit positions and are treated as unique by this code even if they are not unique.
If you only care about uniqueness for lower case letters, and you are sure the code is only used in architectures that have at least 32 bits in an int, then this is a decent approach. Otherwise, when you want an array of bits, use some actual array of bits.

Count Alphabet Characters from an array? [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 8 years ago.
Improve this question
So, in this program we have to
A. Count the number of each letter of the alphabet found in a character array and store these counts in an integer array
I understand that part but then it says we have to use the ASCII values which confuses me.
This is the file for reference:
" A frog walks into a bank and asks the teller, Miss Pattywack
for a loan. Miss Pattywack tells the frog,
"you must have collateral for the loan".
The frog pulls out of his pouch his porcelain people collection
and offers them as collateral.
Miss Pattywack looks at the figurines and tells the frog that
the collection is not acceptable.
The bank manager overhears the conversation and yells to the
teller - "It's a nick nack Pattywack, give the frog a loan!"
Use a for loop to examine each character in the character array
i. Use toupper to case the letter, so you are only dealing with capital letters
ii. If the character is a letter of the alphabet, increment the integer array in the position of the ASCII value of the character minus 65
1) 65 is the ASCII value of letter, 'A'
(1) If the character is A, 65-65 = 0 the position you want to increment for the character A
(2) If the character is C, 67-65 = 2 the position you want to increment for the character C
I have this so far:
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS], int lengthOfArray)
{
int index;
for(index = 0; index <= lengthOfArray; index++)
{
chArray[index] = toupper(chArray[index]);
static_cast<int>(index);
}
}
That's all I have because that's all I understand. I mean, I understand how you get the ASCII value but I am so lost on how to actually make the for loop for this. Like I'm assuming you look at the characters from the file but I don't understand how you get that value you and keep going. I don't know if I make sense but I'm hoping I do and someone can help!! Thanks in advance.
Things you need:
An array to hold the characters that you read. I think chArray is that array.
An array to hold the count of the letters of the alphabet. I don't see that array.
Update the code in CountAlphabetCharacters to go through the characters in chArray and update the count of letters in chArray.
Before calling CountAlphabetCharacters, create the array for holding the count of characters.
int charCountArray[26] = {0};
Change the function CountAlphabetCharacters to accept it as an argument.
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS],
int charCountArray[],
int lengthOfArray)
Use it as an argument in the call to CountAlphabetCharacters.
Update the implementation of CountAlphabetCharacters.
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS],
int charCountArray[],
int lengthOfArray)
{
int index;
int ch;
for(index = 0; index <= lengthOfArray; index++)
{
// The character at the index.
ch = chArray[index];
// Check whether it is an alphabetical character.
if ( isalpha(ch) )
{
// If so, make it the uppercase letter.
ch = toupper(ch);
// Increment the charCountArray for the letter.
charCountArray[ch-'A']++;
}
}
}
void CountAlphabetCharacters(char chArray[MAX_CHARACTERS], int lengthOfArray)
{
int index;
int counter[26]={0}; // holds occurrence of each character
for(index = 0; index < lengthOfArray; index++)
{
if(isalpha(chArray[index]){
int val = (int)toupper(chArray[index]);
counter[val-65]++; //increment occurrence count for this character
}
}
for(char c ='A',index = 0; index <26; index++,c++)
{
printf("%c : %d", c, counter[index]); //print character and corresponding occurrence
}
}

Regular Expression Matching all permutations [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 8 years ago.
Improve this question
I want to find all the permutations of 0123 in the string below
01210210021212333212300213231102023103130001332121230221000012333333021032112
can i have a regular expression that can give me the permutations of 0123 matching in the string ?
Also i need if there are any overlapped patters
"0123" here i want a match of [1023][1230][2301][3012]
Not regex, but C++11:
#include <iostream>
#include <algorithm>
#include <string>
int main()
{
const std::string s("01210210021212333212300213231102023103130001332121230221000012333333021032112");
const std::string ref("0123");
if(ref.length() > s.length())
{
return 0;
}
for(int i = 0; i < s.length() - ref.length(); ++i)
{
if(std::is_permutation(s.cbegin()+i, s.cbegin()+i+ref.length(), ref.cbegin()))
{
const std::string extract(s, i, ref.length());
std::cout << extract << std::endl;
}
}
return 0;
}
To be compiled for example with g++ -std=c++11 -o sample sample.cpp
If you absolutely need regex: (?=[0123]{3})(.)(?!\1)(.)(?!\1|\2)(.)(?!\1|\2|\3). which means:
(?=[0123]{3}) : positive assertion that the 4 next characters are 0, 1, 2, 3
(.) : capture first character
(?!\1) : assert that following character is not the first capture group
(.) : capture second character
(?!\1|\2) : assert that following character is neither the first nor the second capture group
etc.
A regular expression cannot do what you're asking for. It cannot generate permutations from a string.