Sign in to follow this  

Okay, removeing sertain parts of a string

This topic is 4759 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
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
ohh im sorry, im running slackware 10, and programming in C.. tHanks

Share this post


Link to post
Share on other sites
check out strstr. it finds a string inside a string.

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);
}

[/code]
From,
Nice coder

Share this post


Link to post
Share on other sites

#include <string.h>

int func(char * in, char ** out)
{
char * temp = strdup(in);
char * start = strchr(temp, '!');
int r = 0;

if(!start)
start = strchr(temp, '@');
if(!start)
// neither '!' nor '@' occur in the string
r = -1;

*start = 0;

// assume out is a buffer of adequate length
strcpy(*out, temp);
free(temp);
return r;
}


Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
#include <string.h>

int func(char * in, char ** out)
{
char * temp = strdup(in);
char * start = strchr(temp, '!');
int r = 0;

if(!start)
start = strchr(temp, '@');
if(!start)
// neither '!' nor '@' occur in the string
r = -1;

*start = 0;

// assume out is a buffer of adequate length
strcpy(*out, temp);
free(temp);
return r;
}

The highlighted line would cause an access violation if neither '!' not '@' are contained in the string.
#include <string.h>

int func(char * in, char ** out)
{
char * temp = strdup(in);
char * start = strchr(temp, '!');
int r = 0;

if(!start)
start = strchr(temp, '@');
if(!start)
// neither '!' nor '@' occur in the string
r = -1;

else
*start = 0;


// assume out is a buffer of adequate length
strcpy(*out, temp);
free(temp);
return r;
}


Enigma

Share this post


Link to post
Share on other sites

#include <iostream>
#include <string>
#include <conio.h>
using namespace std;

#define NUL '\0'

std::string ParseString(std::string str)
{
int i = 0;
i = str.find_first_of('!');
if(str[i] == '!'){
return str.substr(0,i);
} else{
i = str.find_first_of('@');
if(str[i] == '@'){
return str.substr(0,i);
}
}
return str;
}

int main(int argc, char **argv) {
cout << ParseString("this!ismystring") << endl;
cout << ParseString("this@ismystring") << endl;
return 0;
}//end main()

Share this post


Link to post
Share on other sites
Quote:
Original post by Enigma
The highlighted line would cause an access violation if neither '!' not '@' are contained in the string.
#include <string.h>

int func(char * in, char ** out)
{
char * temp = strdup(in);
char * start = strchr(temp, '!');
int r = 0;

if(!start)
start = strchr(temp, '@');
if(!start)
// neither '!' nor '@' occur in the string
r = -1;

else
*start = 0;


// assume out is a buffer of adequate length
strcpy(*out, temp);
free(temp);
return r;
}


Enigma


It's because of bugs like that (at least partially) that I use early returns from functions (they generally smell better to me than most of the other workarounds):

#include <string.h>

int func(char * in, char ** out) {
char * temp = strdup(in);
char * split = strchr(temp, '!') || strchr(temp, '@');

if(!start) return -1;

*split = 0;

// assume out is a buffer of adequate length
strcpy(*out, temp);
free(temp);
return 0;
}

Share this post


Link to post
Share on other sites
Thanks. I use it when it reads idiomatically. The false-ness of null pointers is quite handy. :D Anyway, it's a good feeling to be able to write decent plain old C code when I haven't actually compiled the stuff in years - even if it does mean dealing with my archnemesis, "char *". ;)

Share this post


Link to post
Share on other sites

This topic is 4759 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this