Jump to content
  • Advertisement
Sign in to follow this  
peb

Okay, removeing sertain parts of a string

This topic is 5009 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
String manipulation questions are easier to answer if you mention what programming language you're using.

Share this post


Link to post
Share on other sites
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 @.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 =)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
#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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!