c - Why isn't my removeString function removing the characters? -
i'm working through exercise in book, i'm not allowed use library functions aside printf
, scanf
.
i have written program takes in string. can input how many characters remove string. , choose @ index, performed.
my attempt @ loop through characters want keep @ end of function , replace them functions want lose.
for reason doing exact opposite.
if input string: "the wrong son"
, choose remove 6 characters , begin @ 4th index of array,
i should get: "the son"
instead, getting "the wro"
.
i have been banging head against 4 hours now.
can please tell me i'm going wrong?
#include <stdio.h> void removestring(char source[], int start, int count); int stringlength(const char string[]); int main(void) { int start, count; char source[20]; printf("what's string?\n"); scanf("%[^\n]s", source); printf("how many characters want remove?\n"); scanf("%i", &count); // choose index start deletion printf("where starting from?\n"); scanf("%i", &start); // call function remove characters string removestring(source, start, count); // print out result, stored in source printf("here's result: %s\n", source); return 0; } void removestring(char source[], int start, int count) { int i, j; // find length of string int length = stringlength(source); //loop through last few characters want keep until hit end of string for(i = (start+count); <= length; i++) { //loop backwards through string, first of ones want keep, stop @ 'start' of index for(j = (start+count)-1; j <= start; j--) { // assign 'i' last few characters want keep , put them in place of characters want delete source[j] = source[i]; } } // assign null character end string once we've finished modifying source[count + 1] = '\0'; } int stringlength(const char string[]) { int count =0; while(string[count] != '\0') count++; return count; }
can please tell me i'm going wrong?
in indexing.
instead of posting simple solution, try make understand went wrong.
for(i = (start+count); <= length; i++)
here, input 'the wrong son', length
equal 13. result indexing source[13]
, not out-of-bounds access, since have statically initialize array 20 cells, it's still exceeding length of input string 1.
so change i < length
.
now loop:
for(j = (start+count)-1; j <= start; j--)
it says start 9 (for example input) , decrease j
until it's less or equal 4, doesn't make sense, meant write:
j >= start
now grab piece of paper , write down input , pointers please:
start = 4 count = 6 length = 13 start start+count | | v v |t|h|e| |w|r|o|n|g| |s|o|n| 0 1 2 3 4 5 6 7 8 9 101112
run algorithm now.
you getting i-th character , overwrite j-th ones, that's why end 'the nnn' after applying fixes, right?
so want go until start
plus have overwritten , should not overwrite again!
as result, change this:
j >= start
to
j >= start + - (start + count);
and should output:
c02qt2ubfvh6-lm:~ gsamaras$ ./a.out what's string? wrong son how many characters want remove? 6 starting from? 4 here's result: son
if that's not case, tell me post whole fixed code.
edit:
my appending of '\0' wrong?
no, it's correct (after @ least fixes proposed in answer). let's check code:
printf("source = %s\n", source); printf("count = %d\n", count); printf("source[count] = %c\n", source[count]); printf("source[count + 1] = %c\n", source[count + 1]); source[count + 1] = '\0';
which gives:
source = sonnnnson count = 6 source[count] = n source[count + 1] = n
so count + 1
equal 7, second 'n' in string, want place null terminator.
Comments
Post a Comment