The code is to shift a binary number to a certain amount of shifts. It gives a result of the number shifted to the left and the right. However, when I run the code, it doesn't shift to the left properly. For example, if I want 2 shifts for 100 it gives 000 instead of 010, and asking for 3 or more shifts doesn't work. And shifting 12345 by 2 gives 34555 instead of 34512. The shift to the right works fine, but shifting to the left isn't working. How do I fix it?
#include <cstring>
#include <iostream>
using namespace std;
void shiftCstring(char pstrMsg[]);
void initCString(char pstrMsg[], int pSize);
void shiftCstring2(char pstrMsg[], int numberOfBits);
void initCString2 (char pstrMsg[], int pSize);
void shiftCstring3(char pstrMsg[]);
void shiftCstring4(char pstrMsg[], int numberOfBits);
int main()
{
char cstrBin1[9];
cout << "Enter 8-bit binary number: ";
cin.getline(cstrBin1, 9);
char cstrBin2[17];
char cstrBin3[17];
initCString(cstrBin2, 16);
strcpy(cstrBin2, cstrBin1);
cstrBin2[9] = '0';
initCString2(cstrBin3, 16);
strcpy(cstrBin3, cstrBin1);
cstrBin3[9] = '0';
int shifted;
cout << "How many times do you want to shift? ";
cin >> shifted;
if (shifted == 1) {
shiftCstring3(cstrBin3);
shiftCstring(cstrBin2);
cout << "After one shift right: "<<cstrBin2 << endl;
cout << "After one shift left: "<<cstrBin3 << endl;
} else if (shifted > 1) {
shiftCstring3(cstrBin3);
shiftCstring4(cstrBin3, (shifted-1));
shiftCstring(cstrBin2);
shiftCstring2(cstrBin2, (shifted-1));
cout << "After " << shifted << " shifts right: "<< cstrBin2 << endl;
cout << "After " << shifted << " shifts left: "<< cstrBin3 << endl;
} else {
cout << "Not a valid number of shifts." << endl;
}
return 0;
}
void initCString(char pstrMsg[], int pSize)
{
for (int i=0;i<pSize;i ) {
pstrMsg[i] = pstrMsg[i 1];
}
}
void shiftCstring(char pstrMsg[])
{
char tmp = pstrMsg[strlen(pstrMsg)-1];
for (int i=(strlen(pstrMsg)-1);i>0;i--) {
pstrMsg[i]=pstrMsg[i-1];
}
pstrMsg[0]=tmp;
}
void shiftCstring2(char pstrMsg[], int numberOfBits)
{
while (numberOfBits>0) {
shiftCstring(pstrMsg);
numberOfBits--;
}
}
void initCString2 (char pstrMsg[], int pSize)
{
for (int i=pSize;i>0;i--) {
pstrMsg[i] = pstrMsg[i-1];
}
}
void shiftCstring3(char pstrMsg[])
{
char tmp = pstrMsg[strlen(pstrMsg) 1];
for (int i=0; i<(strlen(pstrMsg)-1);i ) {
pstrMsg[i] = pstrMsg[i 1];
}
pstrMsg[0] = tmp;
}
void shiftCstring4(char pstrMsg[], int numberOfBits) {
while (numberOfBits>0) {
shiftCstring3(pstrMsg);
numberOfBits--;
}
}
CodePudding user response:
Here's shiftCstring3
fixed.
void shiftCstring3(char pstrMsg[])
{
size_t len = strlen(pstrMsg);
if (len == 0)
return;
char tmp = pstrMsg[0];
for (size_t i = 0; i < len - 1; i)
pstrMsg[i] = pstrMsg[i 1];
pstrMsg[len - 1] = tmp;
}
As indicated above you have many other problems in your code.
CodePudding user response:
How do I fix it?
First fix your problem description. You are trying to rotate a string, not shift it.
Then forget about C and learn C : use std::string
Rotating by N left or right is a simple call to std::rotate
. (Thanks Paul).