En un ejercicio debo encontrar un patrón de un arreglo desde la derecha hacia la izquierda de otro arreglo.
Tengo la funcion get_line para obtener la linea a comparar con el patrón y la funbcion strindex para buscar el patron.
En la función strindex, en el segundo ciclo for al colocar una coma ; como bloque de ese ciclo, osea el bloque no ejecuta nada solo verifica la condición y decrementa las variables, me da un error extraño al usar el debug de Codeblocks las variables a la segunda vez que se llama a ese for toman valores extraños, solo a la segunda vez.
Les dejo la función que sirve, pero no entiendo porque la otra versión del código no sirve.
Version que sirve
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000 /* maximum input line length */
int get_line(char line[], int max);
int strindex(char source[], char searchfor[]);
char pattern[] = "program"; /* pattern to search for */
/* find all lines matching pattern */
int main()
{
char line[MAXLINE];
int found = 0;
while (get_line(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0)
{
printf("%s", line);
found++;
}
return found;
}
/* getline: get line into s, return length */
int get_line(char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
/* strindex: return index of t in s, -1 if none */
int strindex(char s[], char t[])
{
int i, j, k;
for (i = strlen(s)-1; i>=0; i--)
{
for (j=i, k=strlen(t)-1; s[j]==t[k]; j--, k--)
if (k == 0 )
return i;
}
return -1;
}
Version que no sirve fijense en como cambie en strindex el bloque del sgundo for por un punto y coma, el programa compila bien, deberia funcionar
#include <stdio.h>
#include <string.h>
#define MAXLINE 1000 /* maximum input line length */
int get_line(char line[], int max);
int strindex(char source[], char searchfor[]);
char pattern[] = "program"; /* pattern to search for */
/* find all lines matching pattern */
int main()
{
char line[MAXLINE];
int found = 0;
while (get_line(line, MAXLINE) > 0)
if (strindex(line, pattern) >= 0)
{
printf("%s", line);
found++;
}
return found;
}
/* getline: get line into s, return length */
int get_line(char s[], int lim)
{
int c, i;
i = 0;
while (--lim > 0 && (c=getchar()) != EOF && c != '\n')
s[i++] = c;
if (c == '\n')
s[i++] = c;
s[i] = '\0';
return i;
}
/* strindex: return index of t in s, -1 if none */
int strindex(char s[], char t[])
{
int i, j, k;
for (i = strlen(s)-1; i>=0; i--)
{
for (j=i, k=strlen(t)-1; s[j]==t[k]; j--, k--)
;
if (k == 0 )
return i;
}
return -1;
}
Precisamente no funciona porque has añadido un ';' al final del bucle for.
Que un programa compile no es sinonimo de que funcione, es sinonimo de que sintácticamente está bien.
Para que veas porqué ha fallado pon el siguiente código:
int strindex(char s[], char t[])
{
int i, j, k;
for (i = strlen(s)-1; i>=0; i--)
{
for (j=i, k=strlen(t)-1; s[j]==t[k]; j--, k--) {
printf("[ %c -- %c ] [ %i -- %i ]\n", s[j], t[k], j, k);
}
printf("<<< k = %i>>>\n", k);
if (k == 0 )
return i;
}
return -1;
}
Cuándo el segundo bucle llega al final k se decrementa así que cuándo llegas al if(k == 0) k tiene el valor -1.