How to detect the end of a sentence

Hello, I am writing a program that frames each word of a given sentence and gives the following output:


1
2
| We | are | the | GREATEST | Champions! |
+----+-----+-----+----------+------------+


But I am wondering how to detect the end of the sentence if a '\0' is inserted at the end of each word....
Anyone can help?
Thank you!
Sentences always end with a terminating punctuation mark: one of
. (period aka full stop)
! (exclamation point)
? (question mark)

Hope this helps
I don't think this would be a problem, but you might also want to look for stuff like:

!? or ?!
...
Last edited on
I think you should also look for a space following the
. (period aka full stop)
! (exclamation point)
? (question mark)


(because you can find a . (period) in an abbreviation like B.Sc or Dr. and anyway there should be a space between one sentence and the next)

EDIT
Then I thought some more about it - what happens if Dr. occurs in the middel of a sentence followed
by a name for example "Look there is Dr. Martin and his son?" - that's confusing.
Last edited on
Here is what I tried to do, but I only got the right output when I added all of '?' , '!' and '.' at the end... Any clue?

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 <iomanip>

using namespace std ;

int main ()
{
	char chaine[100] ;
	//sentenceEnd = { '!' ; '?' ; '.' } ;
	int i , j ;
	cout << "=================================================================="
		 << "\n=================================================================="
		 << "\nFRAMEWORDS : frames each word of a user-provided list of words"
		 << "\nInput : list of words"
		 << "\nNote : enter empty line to stop interaction loop"
 		 << "\n=================================================================="
		 << "\n==================================================================\n" ;
	cout << "\n<> Enter list of words : " ;
	cin >> chaine ;
	cout << "+" ;
	for ( i = 0 ; chaine[i] != '!',chaine[i] != '?',chaine[i] != '.' ; i++ )
	{
		if ( chaine == '\0' )
			cout << "+" ;
		else
			cout << "-" ;
	}
	cout << "+\n" ;
	cout << setw (5) << chaine ;
	cout << endl ;
	return 0 ;
}


Thanks in advance :-)
closed account (S6k9GNh0)
Well, I think it depends. By each sentence do you mean new line? If so you can check for \n which signifies the end of a line.
'\n' didn't work.. in fact when a user types a sentence, the "enter" key is taken as a signal to start the next instruction, and not as part of the sentence
Try this.

#include<iostream>
#include<string.h>

using namespace std;

int main ()
{
char str[] ="We are the GREATEST Champions!";
char *pch;
pch = strtok (str," ");
while (pch != NULL)
{
cout << pch << " | ";
pch = strtok (NULL, " ");
}
return 0;
}
Thanks a lot!
I'm still working on it
:-)
I think null character is only inserted at the end of a sentence and a character ' ' is inserted between words.
Use gets(string)(or some other option) to input string where space is not treated as null character. You used cin>>chaine; in which when user enters space it is treated as null character and program stops reading furthur.
Last edited on
Thanks AR Khan for your comment. Actually I'm still in earlier stages and haven't came up with a solution yet. Here's what I've done till now:
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
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std ;

int main ()
{
	char chaine[] = "We are the GREATEST Champions!" ;
	char *p , chaineDef[100] ;
	int i ;
	cout << "=================================================================="
		 << "\n=================================================================="
		 << "\nFRAMEWORDS : frames each word of a user-provided list of words"
		 << "\nInput : list of words"
		 << "\nNote : enter empty line to stop interaction loop"
 		 << "\n=================================================================="
		 << "\n==================================================================\n" ;

	//Copy of the contents of chaine inside chaineDef in order to add " " at the beginning
	chaineDef [0] = " " ;
	for ( i = 1 ; chaine[i] != '\0' ; i++ )
		chaineDef [i] = chaine [i-1] ;
	chaineDef [i] = '\0' ;
	
        cout << chaineDef ;
	cout << chaine ;
	

	p = strtok ( chaine , " " ) ;

	while ( p != NULL )
	{
		strcat ( chaineDef , p ) ;
		strcat ( chaineDef , " | " ) ;
		p = strtok ( NULL , " " ) ;
	}
       //I tried to use strcat because I wanted to replace later the letters with '+' 
       //and the spaces with '|'

	cout << chaineDef << endl ;

	return 0 ;
}


This program gives an error on chaineDef [0] = " " ; , and even when I remove it, I still don't get an output on the console
Use chaineDef [0] = ' ' ; instead of chaineDef [0] = " " ;.
Working??? Actually " " is treated as string, can not be stored in single variable whereas ' ' is a single character.
Try this code, it worked perfectly with my compiler.
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
#include<iostream>
using namespace std;
int main()
{
      char chaine[100]; 
      cout<<"Enter string:\n";                
      gets(chaine);                        //Reading string from keyboard
                                           //Treating space as ' ' not as '\0'
      cout<<"After framing:\n";
      //Printing upper part of frame
      cout<<"+";
      for(int i=0;chaine[i]!='\0';i++)
      {
              if (chaine[i]==' ')
              cout<<"+";
              else
              cout<<"-";
      }
      cout<<"+"<<endl;
      
      //Printing middle part of frame
      cout<<"|";
      for(int i=0;chaine[i]!='\0';i++)
      {
              if (chaine[i]==' ')
              cout<<"|";
              else
              cout<<chaine[i];
      }
      cout<<"|"<<endl;
      
      //Printing lower part of frame
      cout<<"+";
      for(int i=0;chaine[i]!='\0';i++)
      {
              if (chaine[i]==' ')
              cout<<"+";
              else
              cout<<"-";
      }
      cout<<"+";
      return 0;
}

Note: My programs are often examples of bad C++,but work properly.
Thanks a lot!!
I've been working on it for a whole week and did not even think that it could be written in such a simple way :-)
Here's the final solution :

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
#include <iostream>
#include <cstdio>

using namespace std ;

int main ()
{
	char chaine[100] ;
	char chaineOut[100] = {0} , line[100] = {0} ;
	int i ;
	cout << "=================================================================="
		 << "\n=================================================================="
		 << "\nFRAMEWORDS : frames each word of a user-provided list of words"
		 << "\nInput : list of words"
		 << "\nNote : enter empty line to stop interaction loop"
 		 << "\n=================================================================="
		 << "\n==================================================================\n" ;

	cout << "\nEnter list of words : " ;
	gets ( chaine ) ;

	chaineOut [0] = '|' ;
	for ( i = 0 ; chaine[i] != '\0' ; i++ )
	{
		if ( chaine[i] == ' ' )
			chaineOut [i+1] = '|' ;
		else
			chaineOut [i+1] = chaine [i] ;
	}
	chaineOut [i+1] = '|' ;
	chaineOut [i+2] = '\0' ;

	line [0] = '+' ;
	for ( i = 0 ; chaine[i] != '\0' ; i++ )
	{
		if ( chaine[i] == ' ' )
			line [i+1] = '+' ;
		else
			line [i+1] = '-' ;
	}
	line [i+1] = '+' ;
	line [i+2] = '\0' ;

	
	cout << endl << line ;
	cout << endl << chaineOut ;
	cout << endl << line ;
	cout << endl << endl ;

	return 0 ;
}
Last edited on
lol, gets

"hehehe pipe this file to your program"

I just had to say it xD
Last edited on
Yeah...don't use gets...use getline() since you are using C++:

getline(std::cin, <some std::string>);
What about strlen() function, with that you can easily know where your sentence ends ?
Last edited on
tried to read about the getline function but don't know yet how to use it, i am always getting errors
anyone can help?
thanks!
Topic archived. No new replies allowed.