how can make this code shorter?

Pages: 12
closed account (oz0i0pDG)
yeah I dont wanna use them cuz it should be in c++
ok so use string. it will be almost a total rewrite, but it will certainly be smaller.
if you are going to do that consider making dictionary an unordered map (?) but I am really not sure what you are doing with the dictionary. It may need to be a vector of string.
Last edited on
closed account (oz0i0pDG)
thank you
Last edited on
The discussions you have on public fora are potentially valuable to others. Deleting the contents of the original post prevents others from benefiting from the discussion.

The original title of this post was "how can make this code shorter?"
And here is the code you posted in the OP.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
bool shrinkable(char word[20], const char dictionary[][20], int rows)
{

    int length = strlen(word);
    char word2[length-1];
 strcpy(word2, word);
   
    length = strlen(word);

    for(int i = 0; i < length; i++){

        for(int i = 0; i < length; i++){
            if(i != length-1){
                word2[i] = word2[i];
            }
            else{
                word2[i] = '\0';
            }
        }


        int ind = 0;
        for(int c = 0; c < length; c++){
            if(c != i){
                word2[ind] = word[c];
                ind++;
            }
        }


        for(int j = 0; j < rows; j++){

            if(strcmp(word2, dictionary[j]) == 0){
                cout << word2 << endl;

                if(strlen(word2) == 2){
                    return true;
                }
                else{
                    return shrinkable(word2, dictionary, rows);
                }
            }
        }
    }
    return true;
}

Last edited on
Topic archived. No new replies allowed.
Pages: 12