Need up to 30 seconds to load.
in this video i'm going to write a c
program that's going to remove a
character from a string at a specified
position in the string
so we'll make a string first we'll say
karas is equal to one two three four
five dash one two three four five
and this could represent something like
a phone number where the first five
digits are the first five digits of the
phone number followed by
a dash followed by the next five digits
in the phone number
and maybe for some reason we want to
take out that dash we just don't need it
for our purposes
so we could write a function that could
solve this problem in the more general
case where we could take out a character
from a string at a specified position
so let's write that function
we'll say here bool
remove car
and the function is going to accept a
string as an argument
as well as a position at which to remove
a character from the string
we're going to have the function return
a bool because it can return true if it
could remove the character and false if
it couldn't
so because we want to use the bool type
we're going to have to include the
stdbool.h library that allows us to do
so
we're also going to include the string.h
library because the string.h library
includes helpful functions for working
with strings
including the stringlength function we
can use to help us write this function
here
so let's provide a definition of the
function down here
so the first thing we're going to do is
actually get the length of the string
we'll say here int length is equal to
strlen
s
and we'll get the length of the string
now we're going to have to check to see
if the position that we're given
is greater than or equal to the length
because if it is it's out of range we
actually can't take that character out
of the string
because it's out of the range of valid
string indexes
so with this string here for example
we have indexes 0 1 2 3 4 5 6 7 8 9 and
this last index here with the 5 is going
to be index 10
but the length of this string is 11
right because you have 5 characters
6 and then 7 8 9 10 11.
so what we want to check is if the
position is greater than or equal to the
length
it's out of range if we did try to say
modify the character at index 11
that would actually be the null
terminator that actually ends the string
we don't want to take that out otherwise
our string is really not a valid string
at that point
so we're going to see here if
the position is greater than or equal to
the length
we're going to return false because we
just can't take that character out of
the string
next we're going to take the character
at
the position we're given here
and we're going to replace it with the
character one over
and we'll do that for all the remaining
characters in the string
essentially shifting the characters up
by one from this position onwards
so we'll say here 4
int i is equal to position
i is less than the length of the string
i plus plus
and we'll say s at i is equal to
s at i plus one
and we're basically going to do
shift
all these characters remaining
over by one
so that the resulting string will kind
of look like this with one two three
four five followed by one two four five
effectively deleting this character from
the string now when we do this shift
because we go right up until the length
here and because we access i plus one
we actually are going to end up shifting
over that final multimeter character as
well which will also terminate the
string one character early as well
so because at this point we've actually
successfully removed the character from
the string we're going to return true
so let's try testing this function out
we'll say here bool
result 1 is equal to and we'll try to
call in the function with the string and
the position or the index 20. and if
this is the case it should be out of
range and so the function should return
false then we'll say if result 1
is true
printf car removed from string
else
will printf car
not removed from
string
in this case we actually want this sort
of failure case here where we couldn't
do it
so we'll save this we'll run it
and we get car not removed from string
and so it worked
so let's now give it a more valid test
here we'll say bool result 2 is equal to
remove car we'll say s and this time
we'll remove the character at position
5. so you have 0 1 2 3 4 5.
position 5 is going to be this dash here
so if result 2 is true
we'll say printf
car removed from string
else we're going to print f and we'll
say
car not removed
from string
and in this case we do want it to be
removed from the string because it is a
valid position there
and then we'll print out the string
afterwards so we'll say printf and we'll
say percent s slash n let's just output
the string
and let's actually just take this one
out we'll comment it out just for
clarity's sake in terms of what we're
looking at in the output
so we'll run this here
and we get car removed from string and
you see it here now one two three four
five one three four five and the dash is
gone
and so we've successfully written a c
program that removes a character from a
string at a specified position checkout
portfolio courses dot com where we'll
help you build a portfolio that will
impress employers including courses to
help you develop c programming projects