2011-04-24 297 views
-2

如何重複字符串? 類似的 「Hello World」 * 3 輸出的 「hello world世界你好Hello World」 的如何在C語言中重複字符串

+0

你需要告訴我們你是怎麼想使用它,如果你有重複串的真正習慣,你可以存儲與結構字符串和重複計數,並在需要時重複該字符串。我猜你想重複內存中的字符串?如果你只需要在輸出時重複字符串,只需要寫三次。 – falstro 2011-04-24 14:06:46

回答

12

在你的源代碼,沒有太多的處理,可能是最簡單的方法是用:

#define HI "hello world" 
char str[] = HI " " HI " " HI; 

這將申報所請求的值的字符串:

"hello world hello world hello world" 

如果你想代碼,將做到這一點,你可以使用類似:

char *repeatStr (char *str, size_t count) { 
    if (count == 0) return NULL; 
    char *ret = malloc (strlen (str) * count + count); 
    if (ret == NULL) return NULL; 
    strcpy (ret, str); 
    while (--count > 0) { 
     strcat (ret, " "); 
     strcat (ret, str); 
    } 
    return ret; 
} 

現在牢記這可以更加高效 - 多strcat操作是成熟的優化,以避免遍地處理數據(一)。但這應該是一個足夠好的開始。

您還負責釋放此函數返回的內存。


(一)如用:

// Like strcat but returns location of the null terminator 
// so that the next myStrCat is more efficient. 

char *myStrCat (char *s, char *a) { 
    while (*s != '\0') s++; 
    while (*a != '\0') *s++ = *a++; 
    *s = '\0'; 
    return s; 
} 

char *repeatStr (char *str, size_t count) { 
    if (count == 0) return NULL; 
    char *ret = malloc (strlen (str) * count + count); 
    if (ret == NULL) return NULL; 
    *ret = '\0'; 
    char *tmp = myStrCat (ret, str); 
    while (--count > 0) { 
     tmp = myStrCat (tmp, " "); 
     tmp = myStrCat (tmp, str); 
    } 
    return ret; 
} 
1

你可以用sprintf。

char s[20] = "Hello"; 
char s2[20]; 
sprintf(s2,"%s%s%s",s,s,s); 
0

http://ideone.com/5sNylW

#include <stdio.h> 
#include <string.h> 
int main(void) { 
    char k[100]; 
    gets(k); 
    int lk=strlen(k); 
    int times; 
    scanf("%d",&times); 
    int tl= times*lk; 
    int i,x=0; 
     for(i=lk-1;i<tl;i++) 
     { 
     k[i+1]=k[x]; 
     x++; 
     } 
    for(i=0;i<tl;i++) 
    { 
    printf("%c",k[i]); 
    } 

    return 0; 
} 
0

您可以嘗試寫自己的功能。它也將與單字符串一起工作(即複製單個字符)。它使用「string.h」中的函數「strcat()」,所以不要忘記包含這個頭文件。

char * 
str_repeat(char str[], unsigned int times) 
{ 
    if (times < 1) 
     return NULL; 

    char *result; 
    size_t str_len = strlen(str); 
    result = malloc(sizeof(char) * str_len + 1); 

    while (times--) { 
     strcat(result, str); 
    } 
    return result; 
} 

但是,如果你只需要打印字符串複製它,嘗試宏

#define PRINT_STR_REPEAT(str, times) \ 
{ \ 
    for (int i = 0; i < times; ++i) \ 
     printf("%s", str); \ 
    puts(""); \ 
} 

結果

PRINT_STR_REPEAT("-", 10); // ---------- 
puts(str_repeat("-", 10)); // ---------- 

PRINT_STR_REPEAT("$", 2); // $$ 
puts(str_repeat("$", 2)); // $$ 

PRINT_STR_REPEAT("*\t*", 10); // * ** ** ** ** ** ** ** ** ** * 
puts(str_repeat("*\t*", 10)); // * ** ** ** ** ** ** ** ** ** * 

PRINT_STR_REPEAT("*_*", 10); // *_**_**_**_**_**_**_**_**_**_* 
puts(str_repeat("*_*", 10)); // *_**_**_**_**_**_**_**_**_**_* 
0

這裏是一種重複的字符串在C,N次。

即有一個字符串「abc」 ,我想長度爲7的字符串,由該字符串重複。

N = 7; 結果: 「abcabca」

while(Index != N){ 

    repeatedString[Index] = oldString[Index%strlen(oldString)]; 
    Index++; 
} 

凡重複的字符串將是 「abcabca」 結尾,並oldString是 「ABC」。

0

我做了基於在這篇文章前面回答了這個功能。 我在這裏分享,因爲一些前面的例子已經扔我出現segfaults

const char* str_repeat(char* str, size_t times) 
{ 
    if (times < 1) return NULL; 
    char *ret = malloc(sizeof(str) * times + 1); 
    if (ret == NULL) return NULL; 
    strcpy(ret, &str); 
    while (--times > 0) { 
     strcat(ret, &str); 
    } 
    return ret; 
}