-
Notifications
You must be signed in to change notification settings - Fork 3
/
MoveAllXAtEnd.cpp
58 lines (43 loc) · 1004 Bytes
/
MoveAllXAtEnd.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
/*
Move All X At End
Take as input a string.
Write a recursive function which moves all 'x' from the string to its end.
*/
#include<iostream>
using namespace std;
int main()
{
void moveX(char [], int);
char str[30];
int len;
cout<<"Enter a string : ";
gets(str);
for(len=0; str[len]!='\0'; ++len); //claculating string length
moveX(str,len);
cout<<endl<<"After moving all x at end, string now is :\n";
puts(str);
return 0;
}
int i=0, j;
char x;
void moveX(char str[], int len)
{
if(str[i]!='\0')
{
if(str[i]=='x'||str[i]=='X')
{
x=str[i];
for(j=i; j<len-1; ++j)
str[j]=str[j+1];
str[j]=x; //shifting x to last position
--len;
++i;
moveX(str,len);
}
else
{
++i;
moveX(str,len);
}
}
}