1
And having a problem with my job, I’m getting her address and I’m going through it, where it’s uppercase I convert to minuscule and wherever there’s a dot, exclamation, interrogation, two dots and a dot and comma.
But according to the Valgrind the problem is in line 82 and 155 of the code, ie in function diminuieeretira();
and in the main
where I am receiving the return of the function diminuieretira();
. I don’t understand what the problem is segmentation fault
.
Below is the code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int contagemCaracteres(char* p) //Declaracao da funcao que conta quantos caracteres existem no texto
{
int i = 0;
while(p[i] != '\n')
{
i++;
}
return i;
}
void contagemPalavras(char* p , int r) //Declaracao de funcao que conta o numero de palavras existentes no texto
{
int palavras = 0, parada = 0, i;
for(i = 0; i < r ; i++)
{
if(p[i] >= 48 && p[i] <= 57 || p[i] >= 65 && p[i] <= 90 || p[i] >= 97 && p[i] <= 122)
{
palavras++;
while(p[i] != 32)
{
parada++;
i++;
}
}
}
printf("%d\n", palavras);
}
int contagemFrases(char* p, int r)
{
int frases = 0, parada = 0, i;
for(i = 0; i < r; i = parada + 1)
{
if(p[i] == 46 || p[i] == 33 || p[i] == 63)
{
frases++;
parada = i;
}
else if(p[i] == 32)
parada = i;
else if (p[i] >= 48 && p[i] <= 57 || p[i] >= 65 && p[i] <= 90 || p[i] >= 97 && p[i] <= 122)
{
if(i == 0)
parada = 0;
else
parada = i;
}
}
return frases;
}
void mediaCaracteres(int c, int f)
{
float media = 0;
media = (float)(c/f);
printf("%.1f\n", media);
}
char* diminuieretira(char** p, int r)
{
int i;
for(i = 0; i < r; i++)
{
if(isupper(*p[i]))
tolower(*p[i]);
else if((*p[i]) == 46 || (*p[i]) == 44 || (*p[i]) == 59 || (*p[i]) == 58 || (*p[i]) == 33 || (*p[i]) == 63);
(*p[i]) == 32;
}
printf("%s", (*p));
return (*p);
}
void retiraespaco(char **p1, int r)
{
int i , j , e = 0, counter = 1;
char* paragrafo2 = NULL;
for(i = 0; j < r; i++)
{
for(j = 0; j < r; j++)
{
if((*p1[j]) != 32)
{
paragrafo2 = (char*) realloc(paragrafo2, counter * sizeof(char));
paragrafo2[i] = (*p1[j]);
counter++;
e = 0;
break;
}
if((*p1[j]) == 32)
e++;
if(e == 1)
{
paragrafo2[i] = 32;
break;
}
}
}
printf("%s\n", paragrafo2);
}
int main (void)
{
int counter = 0, i = 0, recebe1, recebe2, caso;
char* paragrafomod = NULL;
char* paragrafo = NULL;
size_t tamalocado = 0;
getline(¶grafo, &tamalocado, stdin);
do{
scanf("%d", &caso);
switch(caso)
{
case 1: //Primeira etapa
recebe1 = contagemCaracteres(paragrafo);
printf("%d\n", recebe1);
contagemPalavras(paragrafo, recebe1);
recebe2 = contagemFrases(paragrafo, recebe1);
printf("%d\n", recebe2);
mediaCaracteres(recebe1, recebe2);
break;
case 2:
paragrafomod = diminuieretira(¶grafo, recebe1); //Segudna etapa
retiraespaco(¶grafomod, recebe1);
break;
case 3: //Terceira etapa
break;
}
}while(caso != 0);
free(paragrafo);
return 0;
}
A tip, always use intuitive titles to the problem and organize by "topics", so it is more legible.
– Guilherme Nascimento
media = (float)(c/f);
first makes the entire divisionc/f
and sepois converts the result to thefloat
. I think it should bemedia = (float)c/f;
to make the division with floating comma numbers.– pmg
tolower(*p[i]);
does not change value of any variable. You need to do*p[i] = tolower(*p[i]);
– pmg