Skip to content

Instantly share code, notes, and snippets.

@k0001
Created February 25, 2009 02:45
Show Gist options
  • Save k0001/69956 to your computer and use it in GitHub Desktop.
Save k0001/69956 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
/*
* Escribe en ``pos`` la posicion del proximo caracter 'x' en ``s`` a
* donde ``pos`` >=``start``.
* Devuelve 0 si se puede encontrar 'x', de lo contrario, devuelve -1
*/
size_t find_all_x(char *s, size_t start, size_t *pos)
{
size_t i = start;
while (s[i] != '\0') {
if (s[i] == 'x') {
*pos = i;
return 0;
}
++i;
}
return -1;
}
int main(int argc, char **argv)
{
char *s = "4 x 5 + 3 x 2";
size_t start = 0;
size_t i;
while (find_all_x(s, start, &start) == 0) {
printf("%s\n", s);
for (i = 0; i < start; ++i) {
printf(" ");
}
printf("^\n");
++start;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment