Reputation: 87
I'm trying to end my string after a certain character in C. This program will work with the file system so the character will be repeated, I need the find the last occurence of that character end delete everything after that.
I found something from the internet but that doesn't work and I don't know why.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void deleteEnd (char* myStr){
printf ("%s\n", myStr);
char *del = &myStr[strlen(myStr)];
while (del > myStr && *del != '/')
del--;
if (*del== '/')
*del= '\0'; // the program crashes here
return;
}
int main ( void )
{
char* foo= "/one/two/three/two";
deleteEnd(foo);
printf ("%s\n", foo);
return 0;
}
this code basically finds the last '/' character and places the null terminator there. It works theorically but not practically.
By the way if my way is wrong, is there any better way to do this?
Thank you.
**edit: I replaced my code with "strrchr()" upon the suggestions but still got no result:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void deleteEnd (char* myStr){
char *lastslash;
if (lastslash = strrchr(myStr, '/'))
*lastslash = '\0'; // the code still crashes here.
return;
}
int main ( void )
{
char* foo= "/one/two/three/two";
deleteEnd(foo);
printf ("%s\n", foo);
return 0;
}
Upvotes: 3
Views: 4996
Reputation: 16540
to find the last occurrence of a character in a string in C, use the strrchr(3)
function. Its prototype is in <string.h>.
Upvotes: 0
Reputation: 176
In C, When you write literal strings like this: char* foo= "/one/two/three/two";
Those are immutable, which means they are embedded in the executable and are read only.
You get an access violation (crash) when trying to modify read only data.
Instead you can declare your string as an array of chars instead of a literal string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void deleteEnd (char* myStr){
printf ("%s\n", myStr);
char *del = &myStr[strlen(myStr)];
while (del > myStr && *del != '/')
del--;
if (*del== '/')
*del= '\0';
return;
}
int main ( void )
{
char foo[] = "/one/two/three/two";
deleteEnd(foo);
printf ("%s\n", foo);
return 0;
}
Upvotes: 3
Reputation: 2890
You can't change in a string constant, you must copy to a new char string.
#include <iostream>
#include <cstring>
void delete_end(char* dest, const char* source)
{
const char* p = source + strlen(source) - 1;
while(*p != '/' && p > source)
--p;
strncpy(dest, source, p - source);
dest[p-source] = '\0';
}
int main()
{
const char* str = "one/two/three/four";
char buf[256];
delete_end(buf, str);
std::cout << buf << std::endl;
return 0;
}
Upvotes: 0
Reputation: 11
yeschar *lastslash;
if (lastslash = strrchr(myStr, '/'))
*lastslash = '\0'; // the code still crashes here.
return;
}
int main ( void ) {
char foo[]= "/one/two/three/two";
deleteEnd(foo);
printf ("%s\n", foo);
Upvotes: 1
Reputation: 11428
Just declare foo
as an array of chars, instead of a pointer to a char:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void deleteEnd (char* myStr){
char *lastslash;
if (lastslash = strrchr(myStr, '/'))
*lastslash = '\0'; // the code still crashes here.
return;
}
int main ( void )
{
char foo[]= "/one/two/three/two";
deleteEnd(foo);
printf ("%s\n", foo);
return 0;
}
Upvotes: 0
Reputation: 1
Upvotes: 0