Replacing word in char array

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*

Here is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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;
	  }  
      }

      return true;
}


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,
Last edited on
is there a reason your using a char array instead of std::string?
zxrp wrote:
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"...

Example of end product:

ENTER THE TITLE OF THE BOOK: a brief hisTOry OF everyTHING


Correct Output:

A Brief History of Everything


"a" is not supposed to be capitalised but you did it and did not capitalise "of" which is supposed to be

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <sstream>
#include <fstream>
#include <set>
#include <iterator>
#include <cctype>
#include <algorithm>

const std::string ignore("minors.txt");
std::string title(std::string );

int main()
{
    std::cout << "Hello, world!\n"
              << title("a brief hisTOry OF everyTHING") << std::endl;
}

std::string title(std::string intitle) {
    std::istringstream sst (intitle);
    std::ifstream ifs(ignore);
    std::set<std::string> ign {std::istream_iterator<std::string>(ifs), 
                               std::istream_iterator<std::string>()};
    std::string ans;
    
    std::for_each (std::istream_iterator<std::string>(sst), 
                   std::istream_iterator<std::string>(),
                   [&](const std::string& str) {
                       std::string &ref = const_cast<std::string &>(str);
                       std::transform(ref.begin(), ref.end(), ref.begin(), ::tolower);
                       if (ign.count(ref) == 0)
                           ref[0] = toupper(ref[0]);
                       ans += ref + ' ';
                   });
    ifs.close();
    
    return ans;
}
Last edited on
Thanks for the reply @Smac89.

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.
Topic archived. No new replies allowed.