Splitting string using symbols in C -
i have written code trying split long string simpler strings sort them out... when break nested loop, break first loop entirely??
my input "&$(, my,na$me(is"
the output wanted "my na me is" how can solve this??
#include <stdio.h> #include <string.h> #include <ctype.h> int main(){ char splitter[100]; char mystring[1000]; char newstring[1000][1000]; int i,j,z,k=0; scanf("%s", splitter); scanf("%s", mystring); (i=0; i<1000; i++){ (j=k; j<1000; j++){ (z=0; z<100; z++){ if (mystring[j]==splitter[z]){ k++; break; } else { newstring[i][j]=mystring[j]; } } if (mystring[j]==splitter[z]) break; } } (i=0; i<10; i++){ printf("%s ", newstring[i]); } return 0; }
first; c not python, can't use indent denote blocks, must use braces, i.e. {
, }
.
second, no break
breaks closest-most loop in, there's no way break
out of more 1 level.
third, you're looping on strings if they're 100 characters long won't (for instance in example they're not). wrong, should use strlen()
figure out how long are.
fourth, should check return values of scanf()
calls, since can fail.
fifth, newstring
declared array of arrays, i.e. gigantic one-megabyte 2d "square" of characters, not how you're using it.
Comments
Post a Comment