Ofek Pintok
Ofek Pintok

Reputation: 623

sort a string of capital and low letters in c (by swapping)

For example: AAbbCC should be AACCbb

Everything works on the page, but for some reason, when the function swaps the values, the value of str changes (at the end of the running, the program prints the value of AAC)

this is my code:

    void capital_to_Low(char *str)
{

    int i = 0, j = (strlen(str)) -1;

    while (i < j)
    {
        if (str[i] >= 'a' && str[i] <= 'z')
        {
            swap(&str[i], &str[j--]);
        }
        else i++;

    }
    puts(str);
}

void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}

Upvotes: 0

Views: 49

Answers (1)

Karthik Datta
Karthik Datta

Reputation: 36

Change void swap(int *a, int *b) to void swap(char *a, char *b) , because you are referring to the address of the character not integer

Upvotes: 2

Related Questions