I have a problem with substr function(C++) [closed] - c++

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Well, I am supposed to return the middle character of a word. If the word's length is odd, return the middle character. If the word's length is even, return the middle 2 characters.Like "test" => "es" or "testing" => "t".
And This is what I have wroten =>
#include <iostream>
std::string get_middle(std::string input)
{
if (input.length() % 2 == 0) {
return input.substr(1,2);
} else {
return input.substr(1,1);
}
};
int Main() {
get_middle("test");
get_middle("testing");
}

Look at your code, does it return the middle character (in either case)? No because you say input.substr(1,..) it returns the second character.
What you want is this for the odd case
return input.substr(input.length()/2, 1);
I'll leave you to work out the even case.
In fact if you are very clever about it you can have the same formula for both odd and even cases.

First, your main method is incorrect. Kindly change "Main" to "main" and return some integer value.
This is how you can achieve your task.
#include <iostream>
using namespace std;
std::string get_middle(std::string input)
{
if (input.length() % 2 == 0) {
return input.substr((input.length() / 2)-1, 2);
}
else {
return input.substr(input.length()/2, 1);
}
};
int main() {
//cout<<get_middle("test")<<endl;
cout<<get_middle("testing")<<endl;
cout << get_middle("test") << endl;
return 0;
}
Output:
t
es
Now first output is for odd string, and second is for even string.

I am guessing the problem is that you are not getting the middle characters (please state the problem next time). The first parameter should be the index of the first character you want in the substring, meaning input.length() / 2

Related

Caeser's Cipher not working for lower case characters [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 last year.
This post was edited and submitted for review last year and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
Please someone run it , It runs fine for all uppercase values but if you include some x y or z the code breaks.
I found this in hackerrack and several test cases were passed but several failed.
#include<iostream>
using namespace std;
string caesarCipher(string, int);
int main() {
string s;
int places, length;
cin>>length;
cin>>s;
cin>>places;
if(places >26) {
places = places % 26 ;
}
s= caesarCipher(s,places);
cout<<s;
}
string caesarCipher(string S, int k){
for(int i=0;i<S.length();i++){
if(S[i]>='a' && S[i] <='z'){
S[i] = S[i]+k;
if(S[i]>'z'){
S[i]=S[i]-'z'+'a'-1;
}
}
else if(S[i]>='A' && S[i] <= 'Z'){
S[i] = S[i]+k;
if(S[i]>'Z'){
S[i]= S[i]-'Z'+'A'-1;
}
}
else
;
}
return S;
}```
To my own surprise, it looks like strings do not like certain values, not even temporarily.
If you use
S[i] = (S[i]-'a'+k)%26+'a'; instead of
S[i] = S[i]+k;, no value outside of a-z is ever written to the string, which as far as I tested avoids your problem.
An input of "Helloxyz" with a shift of 2 gets an output of "Jgnnqzab".
An the reverse, with a shift of 24 gets "Helloxyz" again.
With that, you do not need
if(places >26) {
places = places % 26 ;
}
/* ... */
if(S[i]>'z'){
S[i]=S[i]-'z'+'a'-1;
}
/* ... */
if(S[i]>'Z'){
S[i]= S[i]-'Z'+'A'-1;
}

while exercise with return in c++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I've just started coding in c++ and now I have an exercise that I can't do because the code seems to not work.
I've to find the max and the min with a sequence of n numbers (in this case i already know that they are 4). I've to use while.
I've just started so I don't know how return properly works...
there aren't syntactical errors but when I run it ask me the number but then it says that the algorithm ends with 0 value.
Here's the code, if you can help me thank you!
#include <iostream>
using namespace std;
main ()
{ float mag,min,i,a;
mag=0;
min=0;
i=0;
while (1)
{
if (i<5)
{ cout<<"insert a number"<<endl;
cin>>a;
if (i = 0)
{ mag=a;
min=a;
}
else
{ if (a<min)
{ min=a;
}
else
{ if (a>mag)
{ mag=a;
}
}
}
i=i+1;
}
else
{ cout<<"maggiore= "<<mag<<endl<<"min= "<<min<<endl;
}
return 0;
}
system ("pause");
}
I see at minimum one problem:
if (i = 0)
This is assignment of i to 0 and compare the result of assignment, which is always false if you assign a 0.
I believe you want only compare and not assign, so you have to use:
if ( i == 0 )
The next problem is
return 0;
This will always end the current function, if the function is main(), it will terminate your program. In your case, you can simply remove the return statement, as in main it will return 0 by default if the function ends.
But if you use
while (1)
without any return, your program runs endless. I don't know what is the expected behavior.
Rest looks fine.
Hint: Your indentation is a bit special. :-)
1st it should be i==0 not i=0 in the if
2nd you should place that return 0 after that cout maggiore or it will close after the first loop
3rd you don't need that system pause there. It does literally nothing. You should either remove it or place it exactly before the return.

Trouble with loops [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
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.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have this simple program to formulate a table of any given number however at the end i want the user to be prompted to either end the program or formulate another table.however the loop does not occur.(i'm only a newbie)
int table(){
int tablenumber;
int tablecount;
cout<<"which number's table would you like to print?"<<endl;
cin>>tablenumber;
cout<<"till which number would you like to multiply it?"<<endl;
cin>>tablecount;
for(int i=0; i<=tablecount; i++){
cout<<tablenumber<<" X "<<i<<"="<<tablenumber*i<<endl;
}
}
int main(){
bool yes=true;
bool no=false;
char answer= yes;
while(answer==true){
table();
cout<<"would you like to formulate another table?(yes/no)"<<endl;
cin>>answer;
}
return 0;
}
The problem is that answer is a char and you are trying to compare it with a bool. true and false are always zero (false) and any non zero number (true), so once you read info into answer the ascii value of the inputted char will not be equal to 0 (The int value for false).
Instead read input and loop while answer equals yes (Or y/Y) since answer is a char. Or make answer a string:
string answer = "yes";
while (answer == "yes" || answer == "Yes") {
//code
}
As a followup on #GBlodgett's answer, I'd like to point out a possible solution:
do{
table();
cout<<"would you like to formulate another table?(yes/no)"<<endl;
cin >> answer;
}while(answer == "yes");
As you can see, you don't even need those two bools. However, since you want the answer to be "yes", you need to make answer a string. Good luck!

Project Euler #5 - Why won't this while loop finish? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
If I change the code line "num++" to "num+=2520", the code runs fine and returns the correct answer, but I'd like to know why it doesn't run as is, primarily because I didn't think of the fact that the number must be a multiple at 2520 before looking the answer up, and I don't see why my own code isn't correctly giving the answer without that change. To me, it seems correct. Unfortunately, the while loop never ends.
My guess is it has something to do with how long the correct number is (232792560), because if I lower the requirements even a little bit (from 9 to 8, per se), the while loop manages to finish.
long long int num = 1;
int div_counter = 1;
bool check = false;
while(!check)
{
for(int i = 2; i < 21; i++)
{
if(num % i == 0)
{
div_counter++;
}
}
if(div_counter == 20)
{
check = true;
}
else
{
num++;
div_counter = 0;
}
}
return num;
You have to reset div_counter to 1 instead of 0.
Your for loop only runs from 2 to 20 inclusive, so if div_counter starts at 0 the max value it can reach is 19.

how to get correct answer merge 2 sorted arrays?! C++ [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i wrote a little algorithm for marge to sorted array. but i have problem with that.
#include <iostream>
using namespace std;
int main() {
// main function started form here:
int firstArray[10] = {1,3,5,7,9,11,13,15,17,19};
int secondtArray[10] = {2,4,6,8,10,12,14,16,18,20};
int mergedArray[20];
int firstCounter=0 , secondtCounter=0 , mergedCounter=0;
while(firstCounter < 10 && secondtCounter < 10){
if(firstArray[firstCounter] < secondtArray[secondtCounter]){
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
} else {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
}
mergedCounter++;
}
while(firstCounter < 10) {
mergedArray[mergedCounter] = firstArray[firstCounter];
firstCounter++;
mergedCounter++;
}
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
for(int j=0; j<20; j++){
//cout << mergedArray[j] << endl;
}
cout << mergedArray[19];
return 0;
}
in outpout for array mergedArray[19] i get something like this: 2686916!!!
i don't know why i get this value. how can i fix that. and why i get this value.
Typo in last while. You may increase your warning level to let your compiler show you your typo (warning: statement has no effect [-Wunused-value]).
while(secondtCounter < 10) {
mergedArray[mergedCounter];
secondtCounter++;
mergedCounter++;
}
should be
while(secondtCounter < 10) {
mergedArray[mergedCounter] = secondtArray[secondtCounter];
secondtCounter++;
mergedCounter++;
}
As pointed out by WhozCraig's comment, you're not assigning any value to mergedArray[19] because you left out the assignment part of the statement.
Since you haven't assigned a value, it's printing out whatever value happens to be at that memory address from previous usage. If you run your program (as it's currently written) several times, you'll see that the number there might change. Also, if you'd printed out the values in mergedArray before assigning anything, you'd see more such meaningless (to you in the current application) numbers.