Caeser's Cipher not working for lower case characters [closed] - c++

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

Related

simple C++ program to calculate number of instances of a substring [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 7 years ago.
Improve this question
can someone point out the error in the following code . I am using a naive approach of comparing both strings character by charcter and updating variable 'u' then comparing it with length of substring . If this is true then variable 'c' is updated by one unit.
Program in C++ :
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int main()
{
char s[50],a[20];
cin.getline(s,50);
cin.getline(a,20);
//int l=strlen(s);
int p=strlen(a);
int i,c=0,j,u=0,k;
for(i=0;s[i]!='\0';i++)
{
if(a[i]='\0')
{break;}
if(s[i]==a[0])
{
for(j=i,k=0;a[k]!='\0';j++,k++)
{
if(s[j]==a[k])
{
u++;
//continue;
}
//else
//break;
}
//cout<<endl<<u;
if(u==p)
{
c++;
}
}
u=0;
}
cout<<endl<<"count "<<c;
getch();
}
For any kind of input combination , I am getting output as 0.
The problem is with this part:
if (a[i] = '\0')
{
break;
}
First, you are using = instead of ==, but that is not the entire problem. Either change a[i] to s[i], or comment out the entire block. I don't see why it is needed.
My tip and my coding convention to avoid your bug that you use = instead of == in:
if(a[i]='\0')
is to put the rvalue in the left side of the operand and the lvalue in the right side, like this:
if ('\0' == a[i])
this convention will avoid bugs like that(you will get a compilation error):
if ('\0' = a[i])
this code will generate a compilation error:
Error C2106 '=': left operand must be l-value

Comparing a String with 2d Array [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
actually I am trying to compare a string with a 2d array.If the entered string is already present in the array the program should terminate.If the string is not present then it should be stored in the next row of array.
what this piece of code is doing is that it is not telling if the entered "cnic" has been previously entered or not. I am using Turbo C++ 3.0 compiler so please keep that in mind.
This program actually takes cnic of user and check whether that cnic has been previously entered or not.
Here is my Program
cout<<"\nEnter Your CNIC?\n";
gets(cnic);
**for (i=0;i<=13;i++)
{
if (strcmp(cnic,cnic2)==0)
{
cout<<"This Cnic has already casted the vote";
}
}
cnic2[i][j]=cnic[i];
j++;**
I used Turbo C (even not ++) but it was long long time ago ... More seriously, I assume :
cnic is a char array of size 13 (12 + terminating null)
cnic2 should be a 2D array of 100 char arrays of size 13 (it is not what is written in your code)
you want to see if cnic C-string is already in cnic2, if it is you reject it, if not you add it.
j is the index of next element in cnic2, and should be explicitely set to 0 before main loop.
Declaration :
char cnic2[100][13];
int found; /* should be bool found but unsure if Turbo C++ knows about that */
(you code was declaring a array of 13 C-Ctring of size 100)
Test loop (including code reviewed and tested by OP) :
/* should control size of nic before that processing - I ASSUME IT HAS BEEN DONE */
found = 0; /* should be found = false; and later use false and true instead of 0 and 1 */
for(i=0; i<j; i++) {
if (0 == strcmp(cnic, cnic2[i]) {
found = 1;
break;
}
}
if (found) {
cout<<"This Cnic has already casted the vote";
continue;
}
if (j == 99) {
count << "Too much Cnic already";
break;
}
strcpy(cnic2[j++], cnic);
/* Revised and Working Code
found = 0; /* should be found = false; and later use false and true instead of 0 and 1 */
for(i=0; i<j; i++) {
if (0 == strcmp(cnic, cnic2[i]))
{
found = 1;
break;
}
}
if (found) {
cout<<"This Cnic has already casted the vote";
continue;
}
if (j == 99) {
cout << "Too much Cnic already";
break;
}
strcpy(cnic2[j++], cnic);
Concentrating on the code you marked with **. The mistakes are numbered below:
for (i=0;i<=13;i++) // (1)
{
if (strcmp(cnic,cnic2)==0) // (2)
{
cout<<"This Cnic has already casted the vote";
}
}
cnic2[i][j]=cnic[i]; // (3)
For mistake (1), you are looping too many times. You want to loop from 0 to 12, or use < 13, not <= 13.
For mistake (2), the second argument to strcmp() is wrong. You want to compare the string starting at cnic2[j].
For mistake (3), you are accessing an element out-of-bounds of both arrays, since i will be 13.
I won't fix these for you, since your code does much more. My answer pointed out was is obviously wrong.

C++ recursive algorithm for permutation [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
The permute() function runs into an infinite loop and I can't seem to find why?
i tried checking the function by removing the recursive call and it seems to be working fine. I also have the base case, so don't know where is the problem.
#include <iostream>
#include <string>
#include <vector>
using namespace std;
string smallString(string s, int k){ // computes a string removing the character at index k
int i,j;
string res;
for(i=0,j=0;j<s.length();i++,j++){
if(i==k){j++;}
res.push_back(s[j]);
}
return res;
}
void permute(string s1, string s2, size_t len){
if(len==1)
{cout<<"length is equal to 1"<<(s1+s2)<<'\n'; return;} //base case
else{
for(int i =0;i<len;i++){
string temp= s2.substr(i,1);
s1.append(temp);
string fin = smallString(s2,i);
//cout<<temp<<'\t'<<s1<<'\t'<<fin<<'\t'<<fin.length()<<'\n';
permute(s1,fin,fin.length());
s1.erase((s1.length()-1));
//cout<<"printing s1 : "<<s1<<'\n';
}
}
}
int main(){
string s2="abc";
string s1="";
permute(s1,s2,s2.length());
return 0;
}
Your problem seems to be in the "smallString" function. In that function, OutOfRange is used in s[j]. I put print like
for(i=0,j=0;j<s.length();i++,j++)
{
if(i==k){j++;}
cout<<"smallString:: s ::"<<s<<'\t'<<"k::"<<k<<'\n';
res.push_back(s[j]); //Problem is here...
}
Now Output print is like
smallString:: s ::abc k::0
smallString:: s ::abc k::0
smallString:: s ::bc k::0
smallString:: s ::bc k::1
smallString:: s ::bc k::1
smallString:: s ::b k::0
smallString:: s ::b k::1
smallString:: s ::b k::1
.
.
So, At one point of time it comes as "s ::b k::1" so you are selecting the character at the position 1 from the string "b".
String is basically an char array which start from 0 to (n-1). For string "b" only 0th position is having character 'b'. but we are accessing an non-existing position.
So it throws error and start looping continuously from here.
FIX FOR YOUR ISSUE:
for(i=0,j=0;j<s.length();i++,j++)
{
if(i==k)
{
j++;
}
else
{
cout<<"smallString:: s ::"<<s<<'\t'<<"k::"<<k<<'\n';
res.push_back(s[j]);
}
}

Unexpected changing of C++ constant integer [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
The variable steady_counter is intialized as a constant integer.
cout << steady_counter;
So long as i have the above statement anywhere before the following code, the function runs as expected and checks if an integer input is or is not a runaround number.
The problem is that when the cout line is not present, the constant integer changes within the below if statements. I tested this by printing steady_counter before entering the if-else, and then after the if-else.
Without the cout line, steady_counter changes to a 4 digit number.
for (int i = 0; i < 10; i++)
{
if (CheckArr[i])
{
num_of_unique++;
}
}
if ((steady_counter == num_of_unique) & (final == NumArr[0]) )
{
return true;
}
else
{
return false;
}
}
Any idea what's going on? Why do I require a cout line to maintain the constant integer steady_counter?
One obvious problem:
for (int i = counter; i > 0; i --)
NumArr[i] = -1;
This covers values from 1 to counter inclusive; while valid indexes for NumArr are from 0 to counter-1 inclusive. So you write outside the array, corrupting something else; possibly another local variable.
Either correct the off-by-one error in the index
NumArr[i-1] = -1;
or use a more canonical loop
for (int i = 0; i < counter; ++i)
or, for more of a C++ flavour,
std::fill(NumArr, NumArr+counter, -1);
There are likely to be further errors, which are better found by using your debugger than by asking people to read through all your code.

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.