Capitalization of characters in a string

Apr 15, 2013 at 3:24am
I need to be pointed in the right direction about the following...

I am reading characters, (text and stuff quotation marks), and doing all kinds of counts of letters and such with it. One thing I must do is convert the first word of each sentence to a capital letter while changing the rest to lower case letters, with the possibility of the input not having a letter as the first character in a word, like "hello.
I am so far able to convert the first letter of EVERY word to a capital letter, but I can't seem to figure out how to manage just the beginning of a sentence.
Here is a sample text from the input file

"SPace! The final FronTIEr.
theSe are the VOyages of tHe
STarshIp enTERprise."

...and my results so far

"space! The Final Frontier. These Are The Voyages Of The Starship Enterprise."

Here is the 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <fstream>

using namespace std;

void getfilename(string,string&);
void format(string&);
int letter (string txt);
int char_count (string charcount);
int sentence (string sentcount); 
int main ()

{

  ifstream input;
  string filename;
  string text;
  char ch;
  int letter_count = 0;
  int word_count = 0;
  int phrase_count = 0;
  int sent_count = 0;
  double avgword;

getfilename(" ",filename);

 cout << endl;

 cout << "Reformatted content of the file named " << filename << endl;

 cout << endl;

 input.open(filename.c_str());

 input >> text;

 while (input)
   {

     letter_count = letter_count + letter(text);

     phrase_count = phrase_count + char_count (text);

     sent_count = sent_count + sentence (text);

     format (text);

     word_count++;

     cout << text <<" ";

     input >> text;

   }

 input.close();

 cout << " " << endl;

 cout << endl;

 cout <<"# words: " << word_count << endl;

 cout <<"# letters: " << letter_count << endl;

 cout <<"# sentences: " << sent_count << endl;

 cout << fixed << showpoint << setprecision (2) << endl;

 cout <<"avg. word length: " << ((double) phrase_count / word_count) << endl;

 cout <<endl;

  return 0;

}

void getfilename(string filetype,string& filename) 
{ 
  cout << "Please enter the name of the input file " << filetype << endl;
  cin >> filename; 
} 

void format(string& word) 
{ 
  word[0] = toupper(word[0]);
 
  for (int i=1; i<word.length(); i++)
      word[i] = tolower(word[i]);
}
 
int letter (string txt)

{
  int count = 0;

  for (int i=0; i<txt.length(); i++)

    if (isalpha (txt[i]))
      count++;
   
  return count;
}

int char_count (string charcount)
{
  int x = 0;

  for (int i=0; i < charcount.length(); i++)

    if (charcount[i] !=' ')
      x++;

  return x;
}

int sentence (string sentcount)
{
  int y = 0;

  for (int i=0; i < sentcount.length(); i++)

    if (sentcount[i] == '!' || sentcount[i] == '.' || sentcount[i] == '?')
      y++;

  return y;
}


Any suggestions?



Apr 15, 2013 at 3:59am
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
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#define isBetween(A,B,C) ( ((A <= B) && (B <= C)) )

bool isLetter( char g )
{
   return isBetween('a', (g | 0x20), 'z');
}

int main()
{
   std::string line;
   std::string::iterator iter;
   std::getline( std::cin, line );
   
   for ( iter = line.begin()+1; iter != line.end(); ++iter )
   {
      if (isLetter(*iter) and !isLetter(*(iter - 1)))
      {
	 *(iter) |= 0x20;
	 *(iter) ^= 0x20;
	 continue;
      }
      *iter |= 0x20;
   }
   
   printf("%s\n", line.c_str());
   
   return 0;
}


Input:
"SPace! The final FronTIEr. theSe are the VOyages of tHe STarshIp enTERprise."

Output:
"Space! The Final Frontier. These Are The Voyages Of The Starship Enterprise."

You get the idea
Last edited on Apr 15, 2013 at 4:03am
Apr 15, 2013 at 6:31pm
Thank you for the response. I think I can make this work.
Topic archived. No new replies allowed.