0

I 'm trying to avoid char repetition in key

// making chars array of alphabets
string alph = "abcdefghijklmnopqrstuvwxyz";

//making sure that there is no character repitition
char rep[26] = "00000000000000000000000000";

//iterating through key
for (int i = 0; i < 26; i++)
{
    // iterating through alphabets
    for (int p = 0; p < 26; p++)
    {
        if (alph[p] == tolower(argv[1][i]))
        {
            atoi(rep[p]) += 1;
        }
        else if (atoi(rep[p]) > 1)
        {
            printf("Key must not contain repeated characters\n");
            return 1;
        }
    }
}

as written in the code, I 've made string of alphabet chars and string of 26-zero chars, and a loop which increases a zero by one when it catches a matching, and if repetition is more than 1, code returns 1. The part which gives error in the code is atoi(rep[p])

3
  • This is essentially a naked code drop - posting of code with no real explanation or details of the problem. It is against forum rules because you're asking others to do the work of determining what is wrong and debugging the code. Please edit the question and add more context. What is it doing wrong? What have you done to try and find the problem? What other info can you provide?
    – Cliff B
    Jun 7 at 2:08
  • Sorry, I 'll do so. Jun 7 at 9:43
  • I 've edited the question. Jun 7 at 13:03
1

For the benefit of all, here's the problem with the atoi issue. Look at this line of code:

        atoi(rep[p]) += 1;

The atoi function returns a value that must be stored somewhere. This line of code is trying to add 1 to... well... what? The integer value that would be returned by atoi is simply not being used. This is not the correct way to use the atoi function.

Instead, one could create an int array of 26 elements and increment the individual elements directly.

1
  • Actually, I 'm wondering why I can't simply pickup a char from the array of chars, convert it to int using atoi, add 1 to it, return it back to char and return it back to the array. Jun 15 at 19:51
0

Problem solved, I implemented a whole-another algorithm. Thanks so much.

1
  • Although it does include a "thank you", it is an answer, more or less. Actually, the only answer at the time it was posted. @MoHassan, please accept your own answer (when permitted) or another answer to close out the question.
    – Cliff B
    Jun 7 at 22:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .