My code lets a user input how big they want an array to be then it reverses it. My professor wants us to shift the element 2 spaces to the left. So if someone entered 12345 into the array my code spits out 54321. Our Professor then wants us to shift the code 2 spaces to the left so my code would then spit out 32154
I asked my professor for help and he said
save the first number to int x;
save the second number to int y;
shift everything 3 positions to the left.
assign y to the last element in the array
assign x to the element next to last.
But Im not quite understanding how to put this into my code
#include <iostream>
usingnamespace std;
int main()
{
int N; // This will be the number the user enters for size of array
char choice; // Choice on if they wish to select another array
cout << "This is an array manipulation program" << endl;
cout << "\nEnter the size of the array ";
cin >> N;
char array[N]; // This is the array that will store all the inputs
do
{
cout << "\nEnter any " << N << " Characters, Digits, or Special Symbols : ";
for(int i=0; i <N; ++i)
{
cin>>array[i];
}
cout << "\n\nThe array contains the following : ";
for(int i=0; i<N; i++)
{
cout<<array[i];
}
cout << endl;
bool CapitalExists=false; // this checks if Capital Letters Exist
bool lowerExists=false; // This checks if small letters exist
bool digit=false; // This checks if the input is a digit
bool special=false; // This checks if the input is a special symbol
for(int i=0; i<N; i++)
{
if (array[i]>= 'A' && array[i]<= 'Z')
{
CapitalExists=true;
cout << array[i] << " is a Capital Letter" <<endl;
}
elseif(array[i]>='a' && array[i]<='z')
{
lowerExists=true;
cout << array[i] << " is a Small Letter" <<endl;
}
elseif(array[i]>=48 && array[i]<=57)
{
digit=true;
cout << array[i] << " is a Digit" <<endl;
}
else
{
special=true;
cout << array[i] << " is a Special Symbol" <<endl;
}
}
if(!CapitalExists)
cout<<"\n\nThere are no capital letters in the array \n" <<endl;
if(!lowerExists)
cout<<"\n\nThere are no small letters in the array \n" <<endl;
if(!digit)
cout<<"\n\nThere are no digits in the array \n" <<endl;
if(!special)
cout<<"\n\nThere are no Special Symbols in the array \n" <<endl;
cout << "\n\n\nThe array in reverse order ";
for(int i=N-1; i>=0;i--)
{
cout<<array[i];
}
cout << "\n\nThe array after converting Capital Letters \nto Small Letters"
<< " and Vice Versa : ";
for(int i=N-1; i>=0;i--)
{
if(array[i]>='A' && array[i]<='Z')
cout<<char(array[i]+=32);
elseif(array[i]>='a' && array[i]<='z')
cout<<char(array[i]-=32);
else cout <<array[i];
}
cout << "\n\nThe array after rotating 2 position to the left " << endl;
int x = array[0]; // Int x is the first number to be shifted
int y = array[1]; // Int y is the second number to be shifted
cout << "\n\nWould you like to check for another array?"
<< "\nEnter Y or y for Yes \nOR\nN or n for No -------> ";
cin >> choice;
while (choice != 'y' && choice != 'Y' && choice != 'N' && choice != 'n')
{
cout << "Invalid Character, Enter Y, or y, or N, or n"
<< " -----> ";
cin >> choice;
}
}
while (choice != 'n' && choice != 'N');
cout << "\n\nProgram is terminated" << endl;
cout << "\nThis Algorithm Designed and Implemented by\n"
<< "Taylor Johnson - Analysis Data Group\nOctober 30 - 2015" <<endl;
return 0;
}
There're too many irrelative code.
My idea to solve the problem is to save the first value, shift everything to the left and then overwrite the last field with saved first value. Repeat the steps to result shifting two characters.
1 2 3 4 5 6 7 8 9 10 11 12
void shift_str(char *str, int count)
{
int i, k, t;
for (i = 0; i < count; i++) {
t = str[0];
for (k = 1; k < strlen(str); k++) {
str[k - 1] = str[k];
}
str[k - 1] = t;
}
}