It should convert a string like this: Example: HEloOO, should be converted into : heLOoo . For some reason it doesn't work,it just wont convert the letters from uppercase to lowercase and vice versa any help would be appreciated ?

#include <stdio.h>
#include <string.h>
#include <ctype.h>

void rek(char array[], int d)
{
    int counter=0;

    if(d==0)
    {
        printf("%s \n",array);
        printf("%d \n",counter);
    }
    else
    {
        if((array[d]>='A' && array[d]<='Z')&&(array[d-1]>='A' && array[d-1]<='Z'))
        {
            array[d]=array[d]+32;
            array[d-1]=array[d-1]+32;
            counter++;
            rek(array,d-2);
        }
        if((array[d]>='a' && array[d]<='z')&&(array[d-1]>='a' && array[d-1]<='z'))
        {
            array[d]=array[d]-32;
            array[d-1]=array[d-1]-32;
            counter++;
            rek(array,d-2);
        }
    }
}

int main()
{
    char array[100];
    int d;

    gets(array);
    d=strlen(array);

    rek(array,d);

    return 0;
}
3

There are 3 answers

4
Prune On

The main problem is that you recur only if your have a pair of upper-case or lower-case letters. Otherwise, you drop off the end of your if, return to the calling program, and quit converting things.

The initial problem is that you've indexed your string with the length. A string with 6 characters has indices 0-5, but you've started with locations 5 and 6 -- the final 'O' and the null character.

The result is that you check 'O' and '\0'; the latter isn't alphabetic at all, so you drop through all of your logic without doing anything, return to the main program, and finish.

For future reference, Here's the debugging instrumentation I used. Also see the canonical SO debug help.

#include<stdio.h>
#include<string.h>
#include<ctype.h>
void rek(char array[], int d)
{
    int counter=0;

    printf("ENTER rek %s %d\n", array, d);

    if(d==0)
    {
        printf("%s \n",array);
        printf("%d \n",counter);
    }
    else
    {
        printf("TRACE 1: %d %c%c\n", d, array[d-1], array[d]);
        if((array[d]>='A' && array[d]<='Z')&&(array[d-1]>='A' && array[d-1]<='Z'))
        {
            printf("TRACE 2: upper case");
            array[d]=array[d]+32;
            array[d-1]=array[d-1]+32;
            counter++;
            rek(array,d-2);
        }
        if((array[d]>='a' && array[d]<='z')&&(array[d-1]>='a' && array[d-1]<='z'))
        {
            printf("TRACE 3: lower case");
            array[d]=array[d]-32;
            array[d-1]=array[d-1]-32;
            counter++;
            rek(array,d-2);
        }
    }
}
int main()
{
    char *array;
    int d;

    array = "HEloOO";
    d=strlen(array);

    rek(array,d);
    printf("%s\n", array);

    return 0;
}
0
Vlad from Moscow On

Your function does not call itself when two adjacent characters have different cases. Also you can get different results when the string is processed from the start or from the end.

I would write the function the following way

#include <stdio.h>
#include <ctype.h>

char * rek(char *s)
{
    if (s[0] && s[1])
    {
        size_t i = 1;
        if (islower((unsigned char)s[0]) && islower((unsigned char)s[1]))
        {
            s[0] = toupper((unsigned char)s[0]);
            s[1] = toupper((unsigned char)s[1]);
            ++i;
        }
        else if (isupper((unsigned char)s[0]) && isupper((unsigned char)s[1]))
        {
            s[0] = tolower((unsigned char)s[0]);
            s[1] = tolower((unsigned char)s[1]);
            ++i;
        }

        rek(s + i);
    }

    return s;
}

int main( void )
{
    char s[] = "HEloOO";

    puts(rek(s));

    return 0;
}

The program output is

heLOoo
0
Count Zero On

I come up with this dirty solution:

#include <iostream>
#include <string>
#include <cctype>

using namespace std;

string solve(const string& str)
{
    if (str.empty()) {
        return "";
    }
    if (str.front() >= 'a' && str.front() <= 'z') {
        return (char)toupper(str.front()) + solve(str.substr(1));
    }
    if (str.front() >= 'A' && str.front() <= 'Z') {
        return (char)tolower(str.front()) + solve(str.substr(1));
    }
}

int main()
{
    string str;
    cin >> str;
    cout << solve(str) << endl;
    return 0;
}