Header Ads Widget

Move hash | Capgemini coding question

Move hash | Capgemini coding question 

Problem:-

You have write a function that accepts, a string that has some “#”, in it you have to move all the hashes to the front of the string and return the whole string back . 

Here You have to complete char* moveHash(char str[],int n); 

Sample input :

easy#coding#zone

Sample output:

##easycodingzone

Move hash | Capgemini coding question

The objective of the code is to move all the hashes at the starting of the string . You just have to complete the function  moveHash(char str[],int n);  


C++ code:-

#include<bits/stdc++.h>
using namespace std;

// function for moving hash
char *moveHash(char str[],int n)
{
for(int i=0;i<n;i++)
{
if(str[i]=='#')
{
int j=i;
while(j>0)
{
char ch=str[j];
str[j]=str[j-1];
str[j-1]=ch;
j--;
}
}
}
return str;
}

// driver function
int main()
{
char s[5000];
cin>>s;
int len=strlen(s);
cout<<moveHash(s,len);
return 0;
}

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-


C-tutorial:-

See more:-