Hey guys, I'm writing a function that accepts a char array containing the title of a book as parameter, the following then has to take place:
1.) Extra white spaces between words need to be removed [DONE]
2.) Text has to be converted to title case, i.e each new word has to start with a capital letter [DONE]
3.) Lastly I have I text file (minors.txt) containing a number of words that should not be capitalized by the function, like "a" and "an", however I don't know how to implement this, any help on how to do this will be much appreciated!
Example of end product:
ENTER THE TITLE OF THE BOOK: a brief hisTOry OF everyTHING
Correct Output:
A Brief History of Everything
[EDIT] - Correct Output:
a Brief History of Everything
*Note, the word "of" is not capitalized, because it is one of the words in the text file that should not be capitalized*
bool Book :: convertToTitleCase(char* inTitle)
{
int length = strlen(inTitle);
bool thisWordCapped = false;
//Convert paramater to lower case and
//Remove multiple white spaces
for (int x = 0; x < length; x++)
{
inTitle[x] = tolower(inTitle[x]);
if (isspace(inTitle[x]) && isspace(inTitle[x+1]))
{
while (isspace(inTitle[x]) && isspace(inTitle[x+1]))
{
int i;
for (i = x + 1; i < length; i++)
{
if(i==(length-1))
{
inTitle[i] = '\0';
}
else
{
inTitle[i] = inTitle[i+1];
}
}
}
}
}
/* Read through text file and identify the words that should not be capitalized,
dont know how, please help! */
//Capitalize the first letter of each word
for (int i = 0; i < length; i++)
{
if ((ispunct(inTitle[i])) || (isspace(inTitle[i])))
{
thisWordCapped = false;
}
if ((thisWordCapped==false) && (isalpha(inTitle[i])))
{
inTitle[i] = toupper(inTitle[i]);
thisWordCapped = true;
}
}
returntrue;
}
I was thinking of maby reading the words in the text file into a string array, and then comparing the two arrays to ensure that when a word is present in a text file, the word is not capitalized, however I don't know if that is possible between a string array and a char array.
I'm quite clueless as to what to do or how it works, any help would be appreciated, thanks!
PS - I'm still relatively new to C++ so please excuse inefficient code,
I accidentally capitalized the "a" in the output sentence. Also, the reason the word "of" is not capitalized is because "of" is one of the words in the text file that should not be capitalized.