well_of_sorrows
well_of_sorrows

Reputation: 27

How to toggle the case of letters in C?

My program should toggle the cases of some letters in some given strings. For example, if the inputs are:

3
hELLo wOrld
hellO wORLD
HelLO WORLd

it must output:

Hello World
Hello World
Hello World

but it does not. I've tried everything I could and it still does not output the correct answers. Here's my code:

#include <stdio.h>

int my_len(char mystring[])
{
  int i, len = 0;
  for (i = 0; i <= 1001; i++)
  {
    if (mystring[i] != '\0')
    {
      len += 1;
    }
    else
    {
      break;
    }
  }
}

char upper_to_lower(char character)
{
    if (character >= 'A' && character <= 'Z')
    {
        return character - 'A' + 'a';
    }
    return character;
}

char lower_to_upper(char character)
{
    if (character >= 'a' && character <= 'z')
    {
        return character - 'a' + 'A';
    }
    return character;
}

int main()
{
    char list[10][1000];
    int i, count;
    scanf("%d\n", &count);
    for (i = 0; i < count; i++)
    {
        char temp[1000];
        gets(temp);
        strcpy(list[i], temp);
    }
    for (i = 0; i < count; i++)
    {
        //char f_temp[1000];
        int j;
        for (j = 0; j < my_len(list[i]); j++)
        {
            if (j == 0)
            {
                printf("%c", upper_to_lower(list[i][j]));
                //f_temp[j] = lower_to_upper(list[i][j]);
            }
            else if (list[i][j-1] != ' ')
            {
                printf("%c", upper_to_lower(list[i][j]));
                //f_temp[j] = upper_to_lower(list[i][j]);
            }
            else if (list[i][j-1] == ' ')
            {
                printf("%c", upper_to_lower(list[i][j]));
                //f_temp[j] = lower_to_upper(list[i][j]);
            }
        }
        //puts(f_temp);
        printf("\n");
    }
    return 0;
}

The codes in the comments was another approach I tried to take, but it also did not help. Any help would be appreciated. (Note: I'm new to C, and I cannot use the string header for some reasons.)

Upvotes: 0

Views: 501

Answers (4)

Tarek Dakhran
Tarek Dakhran

Reputation: 2161

The is a trick that will work with ASCII characters:

int toggleCase(int c) {
    return c ^ ('A' ^ 'a');
}

The explanation is that ASCII code for 'A' is 65 and for 'a' is 97. It means that the distance between UPPERCASE letters and lowercase letters is 32. Thus flipping 5th bit will do the trick.

Upvotes: 2

0___________
0___________

Reputation: 67820

A bit of cleaning:

size_t my_len(const char *mystring)
{
    char *end = mystring;
    while(*end++);

    return end - mystring - 1;
}

int upper_to_lower(int character)
{
    if (character >= 'A' && character <= 'Z')
    {
        character = character + ('a' - 'A');
    }
    return character;
}

int lower_to_upper(int character)
{
    if (character >= 'a' && character <= 'z')
    {
        character = character - ('a' - 'A');
    }
    return character;
}

char *mytolowerString(char *str)
{
    char *tmp = str;
    while(*str)
    {
        *str = upper_to_lower(*str);
        str++;
    }
    return tmp;
}

char *mytoupperString(char *str)
{
    char *tmp = str;
    while(*str)
    {
        *str = lower_to_upper(*str);
        str++;
    }
    return tmp;
}


int main()
{
    char z[] = "aSdFg345345345%&^%%ASD";
    char x[] = "aSdFg345345345%&^%%ASD";

    printf("%s \n%s\n", mytolowerString(z), mytoupperString(x));

    return 0;
}

Upvotes: 1

Eraklon
Eraklon

Reputation: 4288

  1. In my_len you do not return with len.
  2. In if (list[i][j-1] == ' ') and j == 0 cases you should use lower_to_upper function.

Upvotes: 1

kaylum
kaylum

Reputation: 14046

The return is missing in my_len. Need return len; as the last statement in that function.

Upvotes: 2

Related Questions