Program that will output correct sentence stucture

closed account (9L8T0pDG)
Hello,

I need this code to correctly out put a malformed sentence removing spaces and is terminated by a period.

Example: this is a TEST .

Should be : This is a test.

This code does not work right, what have I done? What needs to be done for me to fix it im stumped.

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
#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

void getline(char sentence[], int& size);
void process(char  str[], int size);

int main()
{
  
  char sentence[100];
  int size;
  cout << "Enter a sentence. "  << endl;
  getline(sentence, size);
  cout << endl;
  cout << sentence << endl;

  system ("PAUSE");
  return 0;
}

void getline(char sentence[], int& size)
{
  int haveBlank = false;
  int isFirstLetter = true;
  char i = 0;
  for(int i = 0; '.' != (sentence[i] = cin.get()) && i < 100; i++)
  {
    if('\n' == sentence[i])
      sentence[i] = ' ';
      sentence[i] = tolower(sentence[i]);
      if(isFirstLetter && isalpha(sentence[i]))
      {
        isFirstLetter = false;
        sentence[i] = toupper(sentence[i]);
      }

  if(' ' == sentence[i] && !haveBlank)
    haveBlank = true;
  else if(' ' == sentence[i] && haveBlank)
    i--;
  else if(' ' != sentence[i] && haveBlank)
    haveBlank = false;
  }
  if(i < 99)
    sentence[i+1] = '\0';
  else
  {
    sentence[98] = '.';  
    sentence[99] = '\0';
  }
  size = i;
}
why would you name your function getline when that's already a function in the cin class ?
closed account (9L8T0pDG)
That was dumb of me but it still does not work:

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
#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

void getSentence(char sentence[], int& size);
void process(char  str[], int size);

int main()
{
  using namespace std;
  char sentence[100];
  int size;
  cout << "Enter one sentence. The period is the sentinel to quit."  << endl;
  getSentence(sentence, size);
  cout << endl;
  cout << sentence << endl;

  system ("PAUSE");
  return 0;
}

void getSentence(char sentence[], int& size)
{
  int haveBlank = false;
  int isFirstLetter = true;

  char i = 0;

  for(int i = 0; '.' != (sentence[i] = cin.get()) && i < 100; i++)
  {
    if('\n' == sentence[i])
      sentence[i] = ' ';
      sentence[i] = tolower(sentence[i]);
      if(isFirstLetter && isalpha(sentence[i]))
      {
        isFirstLetter = false;
        sentence[i] = toupper(sentence[i]);
      }

  if(' ' == sentence[i] && !haveBlank)
    haveBlank = true;
  else if(' ' == sentence[i] && haveBlank)
    i--;
  else if(' ' != sentence[i] && haveBlank)
    haveBlank = false;
  }
  if(i < 99)
    sentence[i+1] = '\0';
  else
  {
    sentence[98] = '.';  
    sentence[99] = '\0';
  }
  size = i;
}
something like this would work.

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
/* I need this code to correctly out put a malformed sentence removing spaces and is terminated by a period.

Example: this is a TEST .

Should be : This is a test. */

#include <iostream>
#include <cstring>
#include <cctype>

using namespace std;

void takeLine(char sentence[]);

int main()
{
  char sentence[100];

  cout << "Enter a sentence. ";
  cin.getline(sentence, 99);
  cout << endl;
  takeLine(sentence);

  cin.get(); // is standard, "system ("PAUSE")" wouldnt work on a operating system other than windows.. js
  return 0;
}

void takeLine(char sentence[])
{
    int i = 0;

    sentence[i] = toupper(sentence[i]); // converts first letter to upper case
    i++;

    for (i; i < strlen(sentence); i++)  // converts all the letters after the first one to lower case
        sentence[i] = tolower(sentence[i]);

    cout << sentence << endl;
}


ill try to figure out how to fix the space after the period problem and let you know if i figure it out, im still a noob q:
closed account (9L8T0pDG)
hmmmm interesting way there kapo.

Although that does not delete spaces, at least not for me.

And I am a noob also.
works ! but only with one sentence ^_^

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
void takeLine(char sentence[])
{
    int i = 0;
    char *p;

    sentence[i] = toupper(sentence[i]); // converts first letter to upper case
    i++;

    for (i; i < strlen(sentence); i++)  // converts all the letters after the first one to lower case
        sentence[i] = tolower(sentence[i]);

    p = strtok(sentence, ("' .'")); // sloppy but does the trick

    while (p != NULL ){

        cout << p;

        p = strtok(NULL, ("' .'"));

        if (p == NULL)
            cout << ".";
        else
            cout << " ";
    }
}
Last edited on
closed account (9L8T0pDG)
How did you do that so fast?
trial and error i guess
does it do what you want ?
closed account (9L8T0pDG)
Yeah it does thank you. I guess scrap mine.

Dont you have to:

delete p;
p = NULL;
i dont think so, we didnt put *p on the heap so it should delete itself after the takeLine function returns back to main.
closed account (9L8T0pDG)
Alright thanks Kapo!
Topic archived. No new replies allowed.