You are on page 1of 2

//Write the program that writes the second word (and its length) in the ///array of

characters. Words are separated by spaces, punctuation and ////tabs.

#include <iostream>

using namespace std;

bool isSeparated(char c, char* separators){ // the boolean function with two


arguments
char endOfstring = '\0'; // the end of string is being denoded by the null
character.
char* sepPtr = separators; //the char pointer "sepPtr" is pointing at the
other pointer "separators"
bool ret = false; //the declaration of return to false;
while(*sepPtr != endOfstring){ //
if(c==*sepPtr){
ret = true;
break;
}
sepPtr++;
}
return ret;
}
int charArrayLength(char* a){
int ret = 0;
char endOfstring = '\0';
while(*a != endOfstring){
ret++;
a++;
}
return ret;
}

int main(){
int ret =0;
char endOfstring = '\0';
char theSentence[] = "theko , koetlisi , mofoka";
char separators[] = " \t,.:;?!";
char* strPrt = theSentence;
char* strEnd;

//fing the begining of the second word.


bool isSecondWord = false;
while(*strPrt != endOfstring){
while(isSeparated(*strPrt , separators)) {
strPrt++;
isSecondWord =true;
}
if(isSecondWord){
break;
}
else{
strPrt++;
}
}
// find the of the secont word
strEnd = strPrt;
while(*strEnd != endOfstring){
if(isSeparated(*strEnd, separators)){
*strEnd = endOfstring;
break;
}
strEnd++;
}
cout << strPrt << " (" << charArrayLength(strPrt) << ")" << endl;
}

You might also like