Okay, removeing sertain parts of a string
Hello. i need help with strings again... I need to search for a '!' in the string, if it finds it, it removes the '!' and everything after it... if it dosnt find it, it searches for '@' and removes the '@' and everything behind it... thank you :)
best regards, Max
String manipulation questions are easier to answer if you mention what programming language you're using.
strchr() would be more efficient; it finds the first instance of a given character in a string. Assuming you have a writable string, if strchr() returns non-null, then you can simply put a null in the address returned by strchr() to remove the ! and everything after. Same logic for the @.
Quote:Original post by erjo
check out strstr. it finds a string inside a string.
Interesting.... But how could i use it? Please dont tell me to read manpages, couse i cant understand em.... i already tried.. Can someone find me an example of that? thanx
Quote:Original post by SiCrane
strchr() would be more efficient; it finds the first instance of a given character in a string. Assuming you have a writable string, if strchr() returns non-null, then you can simply put a null in the address returned by strchr() to remove the ! and everything after. Same logic for the @.
char mystring;
strcpy(mystring, "hello!thisismystring");
if(strchr('!', mystring)!=0)
{
//then what?
}
thats how far i came... please help me =)
char *mystring = stdup("hello!thisismystring");
char *substring = strchr(mystring, '!');
if(substring != NULL)
{
substring[0] = '\0';
int newLength = strlen(mystring);
// newLength contains location of end of string.
// Do some memory managment here. If we do not do this
// there is a chance that memory problems could popup
char *temp = mystring;
mystring = (char *)malloc(newLength);
strcpy(mystring, temp);
free(temp);
}
Just wrote the code off the top of my head.
Chris
char *substring = strchr(mystring, '!');
if(substring != NULL)
{
substring[0] = '\0';
int newLength = strlen(mystring);
// newLength contains location of end of string.
// Do some memory managment here. If we do not do this
// there is a chance that memory problems could popup
char *temp = mystring;
mystring = (char *)malloc(newLength);
strcpy(mystring, temp);
free(temp);
}
Just wrote the code off the top of my head.
Chris
#include <stdio.h>
int main()
{
char *mystring = "this!ismystring";
char *substring = strchr(mystring, '!');
if(substring != NULL)
{
substring[0] = '\0';
}
printf("%s", substring);
}
I tried this first, but i get an "segmention fault" i get the segmention fault with the memory stuff too :( please help me
int main()
{
char *mystring = "this!ismystring";
char *substring = strchr(mystring, '!');
if(substring != NULL)
{
substring[0] = '\0';
}
printf("%s", substring);
}
I tried this first, but i get an "segmention fault" i get the segmention fault with the memory stuff too :( please help me
Quote:Original post by peb
#include <stdio.h>
int main()
{
char *mystring = "this!ismystring";
char *substring = strchr(mystring, '!');
if(substring != NULL)
{
substring[0] = '\0';
}
printf("%s", substring);
}
I tried this first, but i get an "segmention fault" i get the segmention fault with the memory stuff too :( please help me
#include <stdio.h>
int main()
{
char mystring[] = "this!ismystring";
char *substring = strchr(mystring, '!');
if(substring != NULL)
{
substring[0] = '\0';
}
printf("%s", mystring);
}
From,
Nice coder
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement