Given a sentence ending in point, count how many words begin with the letter S [closed]

0

Very good.

Compile and everything well, I execute and everything is fine, the problem is that when for example I put a sentence where YES it should tell me at least that there is 1 word that starts with S, the counter follows at 0.

Does someone throw me a cable?

#include <stdio.h>

int main (void) {
    int cont;
    char c;

    printf ("Escribe una frase acabada en punto: ");    
    scanf ("%c", &c);

    cont = 0;

    if ((c == 's') || (c == 'S')){
        cont = 1;
    }

    while (c != '.'){
        if (c == ' '){
            scanf ("%c", &c);
            if ((c == 's') || (c == 'S')) {
                cont ++;
                scanf ("%c", &c);
            }
            else {
                scanf ("%c", &c);
            }
        scanf ("%c", &c);
        }

    }       

    printf ("Hay %d palabras que empiezan por la letra S.\n", cont);
}

In advance, Thanks.

    
asked by Alex Iglesias 24.01.2018 в 23:09
source

3 answers

1

The weird thing is that your program ends:

while (c != '.'){
  if (c == ' '){
    scanf ("%c", &c);
    if ((c == 's') || (c == 'S')) {
      cont ++;
      scanf ("%c", &c);
    }
    else {
      scanf ("%c", &c);
    }
    scanf ("%c", &c);
  }
}

Notice that there is no scanf out of if (c == ' ') , then if the character in question no is a space the program will enter an endless loop ... it will not leave the while because the character is not a period and will not read more characters because the character does not is a space.

In this context I assume that either you have made a mistake in copying the program or you are lying to us and the program does not return 0 or 1 or anything because, directly, it does not work.

I have put it as an answer but it really is a comment ... what happens is that the web does not let you put comments so long and, besides, it would not be readable.

    
answered by 24.01.2018 в 23:48
0

Initialize your counter, ie int cont = 0; And try putting cont + = 1; or cont = cont + 1; And you should use a string for the variable c and not char.

    
answered by 24.01.2018 в 23:23
0

I've retouched the program to make it work.

But I think it will help you. Greetings: P

#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX 1000

int main (void) {
int i,j,cont;
char c[MAX],copi_min[MAX];
printf ("Escribe una frase: ");    
gets(c);

i=0;j=0;
while(c[i]!='
#include <stdio.h>
#include <string.h>
#include <ctype.h>

#define MAX 1000

int main (void) {
int i,j,cont;
char c[MAX],copi_min[MAX];
printf ("Escribe una frase: ");    
gets(c);

i=0;j=0;
while(c[i]!='%pre%')//LA convertire en minusculas para trabajar mas comodo.
{
    copi_min[j]=tolower(c[i]);//Tolower es una funcion de la blioteca ctype.h
    copi_min[j+1]='%pre%';     //combierte los caracteres en minusculas,solo trabaja de uno a uno.
    i++;j++;                
}

cont=0;
if(copi_min[i==0]=='s')
   {
       cont++;
   }
i=0;
while(c[i]!='%pre%')
{
   if(copi_min[i]==' ' && copi_min[i+1]=='s')
   {
       cont++;
   }
   i++;
}    
printf ("Hay %i palabras que empiezan por la letra S.\n",cont);
return 0;
}
')//LA convertire en minusculas para trabajar mas comodo. { copi_min[j]=tolower(c[i]);//Tolower es una funcion de la blioteca ctype.h copi_min[j+1]='%pre%'; //combierte los caracteres en minusculas,solo trabaja de uno a uno. i++;j++; } cont=0; if(copi_min[i==0]=='s') { cont++; } i=0; while(c[i]!='%pre%') { if(copi_min[i]==' ' && copi_min[i+1]=='s') { cont++; } i++; } printf ("Hay %i palabras que empiezan por la letra S.\n",cont); return 0; }

Some doubt comments mela to see if I can answer you.

    
answered by 28.01.2018 в 14:23