-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShift_string.cpp
79 lines (69 loc) · 2.17 KB
/
Shift_string.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#include <iostream>
#include <string.h>
#include <stdlib.h>
using namespace std;
void shiftString(int num,char stringInput[]);
int main(){
int num;
char stringInput[30];
cout<<"Please Enter the Word you want to shift.\n";
cin.getline (stringInput ,30 , '\n');
//char stringInput[]={"Whale"};
cout<<"Please Enter a digit upto which you want your string to be shifted..\n";
cout<<"(Enter + num for Right Shift and - num for left shift.)\n";
cin>>num;
shiftString(num,stringInput);
return 0;
} //main
//<-----------------------------------------------------------------------
void shiftString(int num,char stringInput[]){
int strSize;
int shiftRight; int shiftLeft; int posNum; int l1=0; int stopCode;
char tempArray[30];
while (stringInput[l1]!='\0'){
l1 = l1+1;
strSize = l1;
}
if (num>strSize){
cout<<"Entered Num should be less than the lenght of entered string.\n BYE BYE \n";
stopCode=0;
}
if (stopCode!=0){
for (int i=0; i<strSize; i++){
for (int j=0; j<strSize; j++){
//<FOR RIGHT SHIFT>
if (num>0){
shiftRight = i+num;
if ( shiftRight >=strSize){ //if index increases array bounds.
j = shiftRight%strSize;
shiftRight = j;
}//if
if (j==shiftRight){ // required pos for new entry.
tempArray[j] = stringInput[i];
break;
}//if
}//if.
//<FOR LEFT SHIFT>
if (num<0){
posNum = -1*num;
shiftLeft = i-posNum;
if ( shiftLeft < 0){ //if index increases array bounds.
shiftLeft = -1*shiftLeft;
shiftLeft = strSize-shiftLeft;
j=shiftLeft;
}//if
//shiftLeft = abs(shiftLeft);
if (j==shiftLeft){ // required pos for new entry.
tempArray[j] = stringInput[i];
break;
}//if
}//if.
}// for
}// for
// display the elements.
for (int i=0; i<strSize; i++){
cout<<tempArray[i]<<" ";
}
}//Mega if.
cout<<endl<<endl;
} // func ends.