I'm writing an algorithm in C with netbeans to find asterisks in a string.
int main() {
int M=0, i, j;
scanf("%i",&M);
int pos[M];
char c[M];
scanf("%s", c);
i=0;
j=1;
while(c[i] != '\0'){
if(c[i]=='*'){
pos[j] = i;
j++;
}
i++;
}
printf("Asterisks in positions: \n\n");
for(j=1; j<=i; j++){
printf("%i", pos[j]);
}
return 0;
}
But it doesn't work, it prints a lot of numbers even if M is a small number.