• C

sub string

How can I create a sub string from a char buffer based on the last position of a charcter in the string?
for Example I have a char buffer :

char curdir[ 255]; which is "C:\Program Files\My App\new\ blah"

I want to return a string from the last index of "\".
which would leave a substring of " C:\Program Files\My App\new"

Spunog
spunogAsked:
Who is Participating?
 
PaulCaswellCommented:
Once you've found the '\' character, replace it with a string termination character '\0' and the original 'curdir' string will now read 'C:\Program Files\My App\new' as you wish.

Just in case this is homework (and I am not sure that it is) I will try to avoid actually doing the work for you as this is against EE rules.

Paul
0
 
PaulCaswellCommented:
char * lastBackSlash = strrchr ( curdir, "\\" );

if ( lastBackSlash != NULL )
{
 ...
}

//Paul
0
 
spunogAuthor Commented:
Thanks,

I'm a total novice with this stuff. What would the lines be to create the substring ?

SPunog
0
 
spunogAuthor Commented:
I don't normally work with C and I'm just stuck for time right now. If  you could spell it out I would apreciate it.

Cheers
0
 
PaulCaswellCommented:
>>Once you've found the '\' character, replace it with a string termination character '\0' and the original 'curdir' string will now read 'C:\Program Files\My App\new' as you wish.
This does - sort-of - spell it out.

Paul
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.