Given a string output it in a specific way using recursion? [closed] - c++

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));
}
}

Related

how to condense a string of numbers [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 7 years ago.
Improve this question
char *s = "1234560000000000078999";
unsigned int ss = strlen(s);
vector<int> num;
unsigned int i;
for (i=0;i<ss;i+=2)
{
num.push_back((s[i] - '0')*10 + (s[i+1] - '0'));
}
i'm trying to condense a string that only contains numbers and store it in a int vector
the idea is to take each couple of numbers int the string and combain them into one integer
the problem i had is with numbers that start with zero , for example 1107 only gets stored as 117 and 1100 as 110
the other problem i had is with even numbers ;
any sultions please
thank you
1107 does, indeed, get stored as 11 and 07. When you display the values, show two digits or you won't see the leading 0 on the 07. Same thing with 1100.
As to even numbers, yes, you have to look more carefully at the number of digits that you're dealing with. If ss is odd, start out by just storing the first digit. Then process the rest in pairs. So 117 would be stored as, essentially, 01 and 17.

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.

How to know the position of the character? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How to find the first occurrence of a character from the end, but at the same time indicate the position of the end of the string at which to look for?
Ie we need the following function.
lastIndexOf(char c, int position_from_end);
As will be:
QString s("abcadc");
int i = s.mylastIndexOf('c', 0) //6
.
QString s1("abcadc");
int j = s1.mylastIndexOf('c', 1) //3
In c++, use the std::string method rfind(char c, size_t pos)
The character will be searched for in the part of the string which comes before index pos. (If you were to use the version of rfind which searches for a substring, the first character of the match must come before pos).
pos defaults to string::npos, which is larger than any valid string index and therefore causes the search to start at the end of the string.
It returns string::npos if no match is found.
If you want to specify the offset from the end of the string, you can subtract the offset from the string's length:
std::string s;
//...
size_t p = s.rfind(ch, s.size() - offset);

Convert String Contents to Integers [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
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
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I'm trying to convert the individual contents of a string to integers. I need to take each character from the string and convert it to an integer to add to another. This is not using C++11. Is there a simple way to do it?
if the characters are numbers then the numeral value of each is
num_value(c) = c - '0'
This is only possible because the characters representing numbers are in order in the ASCII table.. All you have to do is loop across the string.
"I need to take each character from the string and convert it to an integer to add to another"
In case you want to calculate the sum of digits stored in std::string object, you could do:
std::string myNum("567632");
int sum = 0;
for (size_t i = 0; i < myNum.size(); ++i)
sum += (myNum[i] - '0');
std::cout << sum;
which outputs 29 (i.e. 5 + 6 + 7 + 6 + 3 + 2)
How about std::accumulate ?
#include<string>
#include<algorithm>
//...
std::string myNum("123456789");
std::cout<<accumulate( myNum.begin(), myNum.end(), 0,
[](int sum,const char& x){return sum+=x-'0'; });

Translation of number into strings [closed]

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 would like to know whether there is a way in which we can print the number alphabetically i.e
123 should be printed as one two three.
The only condition is that we should not reverse the number and we should not use array.
I only know these two ways:
"Reverse the number", that is, taking the last digit and cutting it off. For each cut-off digit, one can use an array to look up the correct string.
using switch and a lot of cases
Any ideas?
for hundreds place:
int hundreds = my_num / 100 //Needs "/", NOT "%"
if(hundreds == 0)
cout << "zero";
else if(hundreds == 1)
cout << "one";
//repeat for 2-9
This process could be tweaked to do the other digits as well. It is also worth mentioning that the if/else block a) could be done with a switch/case if preferred, and b) could pretty easily be made into a separate function to avoid having to repeat the block of code over and over, I just wrote out as much as I did for clarity's sake. Note that this assumes the number you're "translating" is an integer. With integers the "/" operator will return the full quotient WITHOUT the remainder, e.g. 123 / 100 = 1, not 1.23
Not necessarily the easiest route, but you can make a function, say DigitToWord which will take a digit 0, 1, 2, ...etc to its word with a switch statement. Then I recommend using a for loop over the number, continuously dividing by 10 and taking the mod for the loop:
int num; //my number i want to print
int div = pow(10, (int)log10(num)); //find the largest power of 10 smaller than num
while(num > 0) {
int remainder = num%div;
int digit = num/div;
DigitToWord();
num = remainder;
}