I am trying to calculate the number of sentences inside any text on the basis that the end of each sentence may be !
, ?
or .
, but when I used strcmp()
it doesn't work as expected. so if the text contains !
and compared with character constant !
it doesn't give the correct output as 0 as assumed.
Although, I tried to test the outputs to understand what took place led to such result but I couldn't understand so can anyone help ?
Thank you.
here is my code:
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int count_sentences(string text);
int main(void)
{
string text = get_string("text: ");
//printf("%s\n", text);
count_sentences(text);
//printf("%i\n", strcmp("!", "!"));
}
int count_sentences(string text)
{
int string_length = strlen(text);
int num_of_sentences = 0;
const char sent_ind1 = '?';
const char sent_ind2 = '!';
const char sent_ind3 = '.';
//printf("%c %c %c", sent_ind1, sent_ind2,
//sent_ind3);
for (int i = 0; i < string_length; i )
{
int value1 = strcmp(&text[i], &sent_ind1);
int value2 = strcmp(&text[i], &sent_ind2);
int value3 = strcmp(&text[i], &sent_ind3);
if (value1 == 0 || value2 == 0 || value3 == 0)
{
num_of_sentences = 1;
}
//printf("1- %i 2- %i 3- %i i- %c c- %c si0 %c si1 %c si2 %c\n",
//value1, value2, value3, i, text[i], sent_ind1, sent_ind2,
//sent_ind3);
//printf("1- %i 2- %i 3- %i i- %i\n",
//sent_ind1, sent_ind2, sent_ind3, text[i]);
}
//printf("string length equal %i and number of sentences equal %i.\n",
//string_length, num_of_sentences);
return num_of_sentences;
}
CodePudding user response:
If you simply want to count a number of '!'
, '?'
and '.'
in the string you need to compare characters.
size_t count_sentences(string text)
{
size_t nos = 0;
size_t pos = 0;
while(text[pos])
{
if(text[pos] == '!' || text[pos] == '?' || text[pos] == '.') nos ;
pos ;
}
return nos;
}
strcmp
compares the whole strings not looking for the substring in the string. In your case, you do not pass as a second parameter the string only the reference to single char (and it is not a valid string). It is an UB.
CodePudding user response:
These records
int value1 = strcmp(&text[i], &sent_ind1);
int value2 = strcmp(&text[i], &sent_ind2);
int value3 = strcmp(&text[i], &sent_ind3);
does not make a sense. For starters the second arguments of the calls of strcmp do not point to strings.
Secondly even if they would point to strings the result of the calls will be equal to 0 only in one case when these characters '!', '?' and '.' are the last characters of the string text.
Instead of the function strcmp
use functions strcspn
and strspn
.
For example the function can look the following way
#include <stdio.h>
#include <string.h>
size_t count_sentences( const char *text )
{
size_t n = 0;
const char *end_of_sentence = "!?.";
while ( ( text = strcspn( text, end_of_sentence ) ), *text != '\0' )
{
n;
text = strspn( text, end_of_sentence );
}
return n;
}
int main( void )
{
const char *text = "Do you know C string functions? "
"Learn them!!! "
"They are useful.";
printf( "%zu\n", count_sentences( text ) );
}
The program output is
3
CodePudding user response:
In addition to properly comparing a char
with a char
of a string answered elsewhere, consider a different way to count sentences.
How many sentences in these 2 strings?
No end punctuation
Screaming text!!! What???
To get 1 and 2 rather than 0 and 6. use ".?!"
to enable an increment the next time a letter is seen.
size_t count_sentences1(const char *text) {
// Best to use unsigned char for is...()
const unsigned char *utext = (const unsigned char *) text;
size_t num_of_sentences = 0;
int start_of_sentence = 1;
while (*utext) {
if (isalpha(*utext)) {
num_of_sentences = start_of_sentence;
start_of_sentence = 0;
} else if (strchr(".?!", *utext)) {
start_of_sentence = 1;
}
utext ;
}
return num_of_sentences;
}