[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] string replace i C



Hej Peter,
det ser endnu bedre ud, for mit program havde ikke virket. strcat indsætter nemlig på det terminerende 0, der ikke var der i min version. I øvrigt bruger jeg altid calloc til den slags, for så er jeg da sikker på min streng ender uden SIGSEGV.........


/  Søren


Peter Maersk-Moller wrote:

Hej Soeren.

Ser fin ud.

Jeg har lavet en der checker lidt mere. Ogsaa ved jeg ikke om strcat er effektiv.

char* replacestr(char* searchstr, char* patternstr, char* replacestr) {
char* ret_str = NULL;
if (searchstr != NULL) {
if ((where = strstr(searchstr,patternstr)) != NULL) {
int lensearch = strlen(searchstr);
int lenpattern = strlen(patternstr);
int lenreplace = strlen(replacestr);
// if (lensearch >= lenpattern) {
if ((ret_str = (char*) malloc(lensearch-lenpattern+lenreplace+1)) != NULL) {
int len = where-searchstr;
strncpy(ret_str, searchstr, len);
strcpy(retstr+len,replacestr);
strcpy(retstr+len+lenreplace,lensearch+len+lenpattern);
}
// }
}
}
return ret_str;
}


Peter

Søren Sørensen wrote:

Skulle detteher mon ikke kunne lave det trick.......

/* Finds occurrence of str2 in str1 and replaces it by str3.
The result is placed in str4.
Returns 1 if exchange was made, otherwise 0.
*/
int strchg( char *str1, char *str2, char *str3, char *str4 )
{
char *c;
if ((c=strstr(str1, str2)))
{
strncpy( str4, str1, (int)(c-str1));
strcat( str4,str3);
c+=strlen(str2);
strcat(str4, c);
return 1;
}
else
{
strcpy(str4, str1);
return 0;
}
}
M.v.h.
Søren


Peter Maersk-Moller wrote:

Hej.

Jeg soeger et eksempel paa en implenetering af replace i C virkende paa strings vaerende nul-termineret arrays af char.

Med replace forstaar jeg en funkion, hvor man kan soege i str1 efter str2 og erstatte str2 med str3 i str1 og derved faa str4. Lidt i stil med

  str4=`echo $str1 |sed -e 's/'$str2'/'$str3'/g'`

Det er let at soege efter i Google, hvis det var C++, men at soege efter C er lidt anderledes ;-) Tips og tricks modtages gerne.

Med venlig hilsen

P







 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2007-03-01, 02:01 CET [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *