c - How to delete specific words from char array? -
so task char array delete these words starts , ends same letter. , @ point program crashes. here function:
void removesame(char * start) { int i, j, first, last, k; (i = 1; < line_length; i++) { if ( * (start + i) != ' ') { first = * (start + i); (j = i; j < line_length; j++) { if ( * (start + + j + 1) == ' ') { last = * (start + + j); } else { break; } } } (; != j; i++) { k = j - i; * (start + k) = "\0"; } } }
and here full code http://pasted.co/22566eb6
as other mentioned:
changing * (start + i)
start[i]
improves readability
"\0"
should '\0'
or 0 - in c, double quotes used represent strings, , strings memory addresses.
array indexes start 0
also keep in mind strings write in clear in source (ex: "abcba string") constant strings (const char*) , must copied other location in order modify them.
void removesame(char * start) { int i, j, k; (i = 0; start[i];) { // advance while haven't reached string's end if (start[i] != ' ') { (j = + 1; ; ++j) { if (start[j] == ' ' || !start[j]) { // advance until find word if (start[i] == start[j - 1]) { (k = i; (start[k] = start[k + j - i]); ++k); // delete word } else { = j; } break; } } } else { ++i; } } }
Comments
Post a Comment