LR中對字串的處理
{
char * str="testroad";
char * str2 = "tid=231&fid=322";
fun_Factorial();
lr_output_message("%s",fun_Reverse("abcdefg"));
lr_output_message("%s",fun_delspace(" delete space fun"));
lr_output_message("%s",fun_upchar(" upper char fun"));
lr_output_message("%s",fun_strrchr(str,'t'));
lr_output_message("%s",fun_getnum(str2,'&'));
return 0;
}
/**
1. 求100~999以內,這樣一個三位數,該三位數等於其每位數字的階乘之和。即:abc= a! + b! + c!
*/
int fun_Factorial() {
int i, hundreds, tens, units;
long product;
for ( i = 100; i < 1000; i++) {
hundreds = i / 100;
tens = (i - hundreds * 100) / 10;
units = (i - hundreds * 100 - tens * 10);
product = factorial(hundreds) + factorial(tens) + factorial(units);
if (product == i)
{
lr_output_message("%d = %d!+%d!+%d!",i,hundreds,tens,units);
break;
}
}
return i;
}
long factorial(int n){
if (n == 0) {
return 1;
} else {
return factorial(n - 1) * n;
}
}
/***
自定義一個函式,實現將字串反序並在主函式中做輸出;
*/
char * fun_Reverse(char * src){
int len = strlen(src);
char * dest = (char *)malloc(len * sizeof(char));
char * start = dest;
strcpy(dest, src);
src = src + len - 1;
while (*dest !='\0'){
*dest++ = *src--;
}
return start;
}
/***
刪除字串中的所有空格.
**/
char * fun_delspace(char * src){
int len = strlen(src);
char * dest = (char *)malloc(len * sizeof(char));
char * start = dest;
while (*src!= '\0')
{
if (*src == ' '){ //*src == 0x20
src++;
continue;
}
else{
*dest++ = *src++;
}
}
*dest = '\0';
return start;
}
/***
將字串中所有下標為奇數位置上的字母轉換成大寫字母(若該位置上不是字母,則不轉換)
**/
char * fun_upchar(char * src){
int len = strlen(src), i;
char * dest = (char *)malloc(len * sizeof(char)), * start;
strcpy(dest, src);
start = dest;
do {
if ((*dest >= 'a' && *dest <= 'z') && i % 2 == 1) {
*dest = *dest - 32;
}
i++;
} while (*dest++);
return start;
}
/***
重寫srtrchr函式,實現查詢字串中指定字元最後一次出現時,所有的字元輸出,將其中指定字串的小寫字母變成對應的大寫字母並輸出
****/
char *fun_strrchr(char *str,char a){
int len=strlen(str);
int t_len;
char * tmp =(char *)malloc(len*sizeof(char));
str=str+len-1;
while(len--){
if (*str==a) {
break;
}
str--;
}
strcpy(tmp, str);
*tmp = *tmp - 32;
return tmp;
}
/***
當我們通過關聯,會得到如下的字串“tid=231&fid=322”,其中tid的值有可能是三位數,也有可能是2位數,不能確定,請編寫一個函式,截取出“fid=322”的值
****/
char *fun_getnum(char *str,char a){
int i,len;
len = strlen(str);
str=str+len-1;
while(len--){
if (*str==a) {
break;
}
str--;
}
return str+1;
}
/**
對字串“jieofAoo)))eEe**7dfUe^^fds^%dfs%”進行處理,統計出非法字元的個數(字母,數字以外的字元都為非法字元)。
**/
Action()
{
char * str ="jieofAoo)))eEe**7dfUe^^fds^%dfs%";
int cont;
int len;
len = strlen(str);
lr_output_message("字串個數為:%d",len);
while (*str != '\0') {
if(*str<=57&&*str>=48){
len--;
}else if(*str<=90&&*str>=65){
len--;
}else if(*str<=122&&*str>=97){
len--;
}
str++;
}
lr_output_message("字串的非法字元的個數為:%d",len);
return 0;
}
/**
*
* 通過關聯,我們獲取到了一個字串"192,000,123",現在要求去掉字串中的逗號
* ,並替換成兩個%%,即:192%%000%%123請編寫一個函式來實現它
* */
Action()
{
char * str ="192,000,123";
int len,i;
char *desc;
len = strlen(str);
desc=(char *)malloc(2*len*sizeof(char *));
fun_No2(str,desc);
lr_output_message("轉換後的字串為:%s",desc);
free(desc);
return 0;
}
char *fun_No2(char *str,char *desc){
while(*str!='\0'){
if(*str == ','){
*desc++='%';
*desc++='%';
str++;
}
*desc++=*str++;
}
*desc='\0';
return desc;
}
/**
*
* 編寫一個函式,查到1至10000內,能被7或者11整除,
* 同時不能被7和11同時整除的數,並放到陣列中打印出來
* */
Action()
{
fun_No3();
return 0;
}
fun_No3(){
int arry[1000];
int i,j,n;
for (i=0;i<1000;i++) {
if( (i%7==0 || i%11==0 ) && (!( i%7==0 && i%11==0 ))){
arry[n]=i;
n++;
}
}
for(j=0;j<n;j++){
lr_output_message("%d",arry[j]);
}
}
/**
*
* 定義一個函式,實現對字串做如下操作:當字元為字母時,
* 對應的ASCII值加5,當字元為數字時,原樣輸出
* ,當有其它字元出現時,結束操作,返回已處理的字串(需要用指標的方式來實現)
* */
Action()
{
char *str = "aee$8873#ee";
char *desc;
int len=strlen(str);
desc=(char *)malloc(len*sizeof(char *));
fun_No4(str,desc);
lr_output_message("處理後的字串為:%s",desc);
free(desc);
return 0;
}
char *fun_No4(char *str,char *desc){
while (*str != '\0') {
if((*str<=90&&*str>=65)||*str<=122&&*str>=97){
*desc=*str+5;
desc++;
str++;
}else if(*str<=57&&*str>=48){
*desc=*str;
desc++;
str++;
}else
{
*desc='\0';
break;
}
}
return desc;
}
/**
*
*通過對資料庫的查詢,我們得知,資料庫對使用者所在的省市縣存成了一個欄位,
*規則如下:以大寫字母來區分啟始字母。如HebeiShijiazhuangXinghua
*表示 河北省石家莊市新華區,又如ZhejiangHanzhouXihu 表示
*浙江省杭州市西湖區,現指令碼中只需要獲取到市一級的行政單位,如Shijiazhuang\Hanzhou
*等,請編寫一個函式來實現
* */
Action()
{
char *str = "HebeiShijiazhuangXinghua";
char *desc;
int len=strlen(str);
desc=(char *)malloc(len*sizeof(char *));
fun_No5(str,desc);
lr_output_message("處理後的字串為:%s",desc);
free(desc);
return 0;
}
char *fun_No5(char *str,char *desc){
int n;
while (*str != '\0') {
if((*str<=90&&*str>=65)){
n++;
if(n==2){
*desc++=*str++;
while (*str>90){
*desc++=*str++;
}
}
}
str++;
}
return desc;
}
/**
*
在一個訂單指令碼的處理過程中,我們得到了一串如下的字串"GZPI655768",
*其中"GZPI"代表了產品的縮寫,後面的數值是產品編號。
*現在指令碼需要提交一個訂單驗證號,以防重複提交訂單,
* 此驗證號的規則是:
* 產品編號+當前日期(請利用lr_save_datetime()函式獲取時間,
*使用方法請在F1中檢視使用方法)證號的生成。
*(不一定需要自定義函式,能拼接出來即可)
* 注:日期的格式如下:20151105070656(年月日時分秒) * * *
* */
Action()
{
char *str = "GZPI655768";
char *newstr = (char *)malloc(strlen(str)*sizeof(char)+1);
char *new_date_str=(char *)malloc(strlen(str)*sizeof(char)+10);
fun_No6(str,newstr);
lr_output_message("產品編號為:%s",newstr);
lr_save_datetime("%Y%m%d%H%M%S",DATE_NOW , "newdate");
strcpy(new_date_str,newstr);
strcat(new_date_str,lr_eval_string("{newdate}"));
lr_output_message("訂單驗證號為:%s",new_date_str);
free(newstr);
free(new_date_str);
return 0;
}
fun_No6(char *str1,char *newstr1) {
int i,len;
len = strlen(str1);
for (i=0;i<len;i++) {
if (*str1>='A'&&*str1<='Z') {
str1++;
}else{
*newstr1++=*str1++;
}
}
*newstr1 = '\0';
}