Last active
August 29, 2015 13:58
-
-
Save PanNyaa/10014441 to your computer and use it in GitHub Desktop.
文字列中から指定された文字列を全置換する関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdarg.h> | |
#include <stdlib.h> | |
int strrep(char*,char*,char*); | |
//文字列から特定文字列を検索し指定の特定文字列に置き換える、ような関数。 | |
//文字列が入った配列の大きさを指定して、置換結果がそれ以上になったらエラーを返します。 | |
//置換後の文字数が同じもしくは少ない場合のみの対応です。 | |
//大きくしてしまうと2次元以上にしていたときなどに、他の配列に食い込んでしまうので、 | |
//元のサイズよりも大きくなったときはエラーを返します。(-1 か 必要なサイズ?) | |
int strrep(char *str,char *search,char *replace){ | |
char *p; | |
int len,len_s,len_r,r_cnt; | |
len = strlen(str); //検索先文字列の長さを得る | |
len_s = strlen(search); //指定された検索する文字列の長さを得る | |
len_r = strlen(replace); //指定された置換する文字列の長さを得る | |
r_cnt = strcnt(str,search); //指定された検索する文字列の数を得る | |
if(len_s < len_r){ | |
return -1; //検索文字列よりも置換文字列のほうが多かった場合、結果配列が大きくなってしまうのでエラーを返す | |
} | |
if(len_s == 0){ | |
return -1; //検索文字列が0文字だった場合、置換処理をする必要がないのでエラーを返す | |
} | |
for(;;){ | |
if( (p = strstr(str,search)) != NULL ){ //検索先文字列から指定文字がHITしたら | |
strncpy(p,replace,len_r); //置換文字数分だけ置換 | |
strncpynull(p+len_r,p+len_s,strlen(p+len_s),0); | |
} | |
if(--r_cnt <= 0) | |
break; //検索文字列の回数分だけ、繰り返す | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdarg.h> | |
#include <stdlib.h> | |
int strcnt(char*,const char*); | |
//文字配列内に指定した文字列がいくつ存在するかを返す関数 | |
int strcnt(char *str,const char *substr){ | |
char *p; | |
int len,i=0; | |
len = strlen(substr); | |
for(;;){ | |
if( (p = strstr(str,substr)) != NULL ){ //検索先文字列から指定文字がHITしたら | |
i++; | |
str = p+len; | |
}else{ | |
break; | |
} | |
} | |
return i; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <stdarg.h> | |
#include <stdlib.h> | |
#define strncpynull( ... ) strncpynull_( __VA_ARGS__ ,1 ) | |
int strncpynull_(char*,char*,size_t, ... ); | |
//strncpyっぽい機能+最後に'\0'を付け足す関数 | |
//flagが0だった場合、_Destの'\0'を検知しなくなる。省略すると1 | |
int strncpynull_(char* _Dest,char* _Source,size_t Count, ... ){ | |
va_list list; | |
char *p_Dest,*p_Source,flag; | |
if(_Dest == NULL || _Source == NULL || Count == 0)return -1; | |
p_Dest=_Dest; | |
p_Source=_Source; | |
va_start( list , Count ); | |
flag = va_arg(list,char); | |
if(flag==1){ | |
while(Count--){ | |
if(*p_Dest=='\0' || *p_Source=='\0')break; | |
*p_Dest = *p_Source; | |
p_Dest++;p_Source++; | |
} | |
if( !(*p_Dest=='\0' || *p_Source=='\0') )*p_Dest = '\0'; | |
}else{ | |
while(Count--){ | |
*p_Dest = *p_Source; | |
p_Dest++;p_Source++; | |
} | |
*p_Dest = '\0'; | |
} | |
va_end(list); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment