Home Forums C Programming Please Help with this Question Re: Re: Please Help with this Question

#3482
GWILouisaxwzkla
Participant

Not quite sure what you needed so I wrote this to read words from the keyboard into a character array. I haven’t tested this code much and the logic could probably be simplified….



/****************************************************************
* File Name : c:programshelptempCG.cpp
* Date : November,17,2008
* Comments : new project
* Compiler/Assembler :
*
*
*
*
*
* Program Shell Generated At: 7:02:58 p.m.
=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/


#include < iostream.h >
//#include < string.h >
//#include < conio.h >
//#include < math.h >
//#include < iomanip >
#include < ctype.h >
#define MAX_LENGTH 50



//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ FUNCTION PROTOTYPES @@@@@@@@@@@@@@@@@@@@@@@@@@



//##################################################################################


//main function ******************************

int main ( )
{

char inputStr [ MAX_LENGTH ];
int i = 0;
cout << "Enter a string: " ;
cin.getline ( inputStr , MAX_LENGTH );
if ( inputStr [ 0 ] == 0 )
return 0; //empty string

cout << "output: " ;
while ( inputStr [ i ] != 0 )
{

if ( inputStr [ i ] == 'y' && ( inputStr [ i + 1 ] == 0 || isspace ( inputStr [ i + 1 ] ) ) )
{
cout << "ies" ;
i ++;
}
else if ( inputStr [ i ] == 's' && ( inputStr [ i + 1 ] == 0 || isspace ( inputStr [ i + 1 ] ) ) )
{
cout << "ses" ;
i ++;
}
else if ( inputStr [ i ] == 's' && inputStr [ i + 1 ] == 'h' )
{
if ( inputStr [ i + 2 ] == 0 || isspace ( inputStr [ i + 2 ] ) )
{
cout << "shes" ;
i += 2;
}
else
cout << inputStr [ i ++ ];

}
else if ( inputStr [ i ] == 'c' && inputStr [ i + 1 ] == 'h' )
{
if ( inputStr [ i + 2 ] == 0 || isspace ( inputStr [ i + 2 ] ) )
{
cout << "ches" ;
i += 2;
}
else
cout << inputStr [ i ++ ];

}

else if ( isalnum ( inputStr [ i ] ) && ( isspace ( inputStr [ i + 1 ] ) || inputStr [ i + 1 ] == 0 ) )
{
cout << inputStr [ i ] << "s ";
i ++;
}
else
cout << inputStr [ i ++ ];


}
cout << endl;

return 0;

}




output:


Enter a string: church mickey sucks
output: churches mickeies suckses
Press any key to continue