Code:
#include <iostream>
using namespace std;
int main()
{
char firstname;
char finalFirstname[20];
cout << "Enter your first name: ";
int num = 0;
int check = 0;
while(firstname = cin.get())
{
if(firstname == '\n')
break;
if(firstname == ' ')
//{
// if(check == 0)
// {
// if(num != 0)
// {
// finalFirstname[num] = '-';
// num++;
// check = 1;
// }
// }
//}
//else
continue;
finalFirstname[num] = firstname;
num++;
}
for(int j=0; j<num; j++)
{
cout << finalFirstname[j];
}
cout << endl;
return 0;
}
1) if there is a space between the 2 words, a hyphen is replaced.
2) if there is spaces in front of or after surnames, try all them.
the 2) was done by running the above script.
the lines commented are used to replace the space between 2 character (not space) with hyphen, but it fails to run. Does the problem lie in the ambiguous else? else should follow the 2nd if-loop.
Pls help!!
Comment