没有重复的字符串组合C

时间:2017-12-04 00:00:20

标签: c algorithm

`#include <stdio.h>
 void convert(int num){
char *word[10];

switch(num){
    case 0: word [0] = "ZERO";
    break;
    case 1: word [1] = "ONE";
    break;
    case 2: word [2] = "TWO";
    break;
    case 3: word [3] = "THREE";
    break;
    case 4: word [4] = "FOUR";
    break;
    case 5: word [5] = "FIVE";
    break;
    case 6: word [6] = "SIX";
    break;
    case 7: word [7] = "SEVEN";
    break;
    case 8: word [8] = "EIGHT";
    break;
    case 9: word [9] = "NINE";
    break;
}
printf("%s", word[num]);


}



int main(){
int a=0;
int b=0;
int c=0;



for(a=0;a<10;a=a+1){
    for(b=0;b<10;b=b+1){
        for(c=0;c<10;c=c+1){
        convert(a);convert(b);convert(c);
        printf("\n");
        }
    }
}´ 

我希望用字符串“0”“1”“2”等重复3到3的所有可能的组合,并将它们中的每一个存储在一个字符串数组中(从“zerozerozero”到“nineninenine”) “)。 现在这就是我所拥有的,我只打印每个组合,并且我很难将每个组合存储在一个数组中。 我将不胜感激任何帮助或建议。 :d

1 个答案:

答案 0 :(得分:0)

我们可以声明一个足够大小的char数组。对于三个单词的连接,我们使用p来查找下一个单词插入位置。

#include <stdio.h>
#include <string.h>
int convert(int num, char* full_word, int p){
    const char* word;
    switch(num){
        case 0: word = "ZERO";
                break;
        case 1: word = "ONE";
                break;
        case 2: word = "TWO";
                break;
        case 3: word = "THREE";
                break;
        case 4: word = "FOUR";
                break;
        case 5: word = "FIVE";
                break;
        case 6: word = "SIX";
                break;
        case 7: word = "SEVEN";
                break;
        case 8: word = "EIGHT";
                break;
        case 9: word = "NINE";
                break;
    }
    strcpy(full_word + p, word);
    p += strlen(word);
    return p;
}



int main(){
    int a=0;
    int b=0;
    int c=0;
    int ind = 0;

    char arr[1000][20];
    for(a=0;a<10;a=a+1){
        for(b=0;b<10;b=b+1){
            for(c=0;c<10;c=c+1, ++ind){
                char word[100];
                int p = convert(a, word, 0);
                p = convert(b, word, p);
                convert(c, word, p);
                strcpy(arr[ind], word);
            }
        }
    }
    for (int i = 0; i < 1000; ++i) {
        printf("%s\n", arr[i]);
    }
    return 0;
}