HELLO;
see this quastion , i tried to solve it , but there is sometjing wrong , i do not know it
because no errors accur !

A 5 letter word x is hidden in a string y such that the first two letters of x are the first two letters of y and the third letter of x is the middle letter of y and the last two letters of x are the last two letters of y. Note that y is any string with length greater than 5.
For example, the 10-letter string grabcefgat hides the 5-letter word great.
And the 7-letter string enatber hides the 5-letter word enter.this is the code :

string extract(string y)
{ int len=y.size();
string x;

for (int i=0;i<5;i++)
{


switch(i)
{
case 0: x[i]=y[i];break;
case 1:x[i]=y[i];break;
case 2:x[i]=y[len/2];break;
case 3:x[i]=y[len-1];break;
case 4:x[i]=y[len-2];break;
}

}

return x;

}


int main ()
{ 
	string a,b;
cout<<"Enter a string :";
cin>>a;

b=extract(a);
cout<<"\n\nThe hidden word is "<<b<<endl;
	


return 0;  }

you don't need a loop or switch statement. use string's substr() method to extract the first two characters and again to get the last two characters.

I'm not sure that x has any memory declared for it before you call the switch statement, in which case the += operator might work better than the = operator. x += y[whatever]; I'm pretty sure you can append a char to an STL string object, though I don't have a compiler handy to prove it.

The switch statement is superfluous (not needed).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.