2015-11-05 177 views
-5

如何對用戶定義的函數進行編碼,該函數用字符串搜索並替換另一個字符串中包含的任何字符的字符出現。 不能在代碼中使用任何字符串變量,必須是用戶定義的函數。 感謝 這就是我試圖到目前爲止 的#define _CRT_SECURE_NO_WARNINGS 的#include 的#include用字符替換字符串中的字符串

void s1(); 
void s2(); 

int main(void) 
{ 

    int i=0; 


    s1(); 
    s2(); 
    printf("c = {'$'} "); 

}//main 
void s1(){ 
    int i = 0; 
    while (i <= 40){ 
    printf("%c", (rand() % 25) + 'A'); 
    i++; 
    } 
} 
void s2(){ 
    char s2[20]; 

    printf("\nEnter a string of minimum 2 and maximum 20 characters= "); 
    gets(s2); 
    puts(s2); 
} 

/* 我只是需要做的是搜索S1並替換任何字符出現的任何其他功能是包含與可以是任何東西(如 '$')字符S2

*/

+0

呀,啓動你的啦ptop,使用文本編輯器並開始輸入。這就是你寫代碼的方式。疑問? –

+0

你試過了什麼?至少在SO中尋找類似的問題。我相信有很多。 – SKD

+0

他們有很多,但他們使用替換函數或任何其他字符串庫函數,如strlen等,我不得不在這個程序中使用。 –

回答

0
//If I have understood your question then this should be answer 
char *replace(char [] a, char b[], int lower, int upper){ 
    char c[100]; 
    int j = 0; 
    for(int i = 0; i < lower; i++){ 
    c[j] = a[i]; 
    j++; 
    } 
    for(int i = 0; i < strlen(b); i++){ 
    c[j] = b[i]; 
    j++; 
    } 
    for(int i = upper; i < strlen(a); i++){ 
    c[j] = a[i]; 
    j++; 
    } 
    c[j] = '\0' 

    for(int i = 0; i < strlen(c); i++){ 
    a[i]= c[i]; 
    } 
    a[i] = '\0'; 
    return a; 
} 
+1

您建議的「替換」僅僅是複製。什麼是'len'?分號過時了嗎?而「獲取」並不是21世紀的閱讀方式。 –

+0

感謝您的幫助,但我試圖避免字符串庫函數像strlen和不使用指針。 –