Home > Software design >  How to change uppercase letter to its next letter keeping lowercase letters unchanged in array?
How to change uppercase letter to its next letter keeping lowercase letters unchanged in array?

Time:12-31

I was trying to solve a question regarding how to change an uppercase letter to its next letter and keeping the lowercase letters unchanged. At first I tried to check if there's any capital letter exists in the string and then I should try to change it to lowercase letter. But I think that there's something wrong in my code. Here's my piece of code:

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
   char alpha[100];

   printf("Enter String: ");
   gets(alpha);

   for (int i = 0; alpha[i] != 0; i  )
   {
       if (alpha[i] >= 'A' && alpha[i] <= 'Z')
       {
           //upperL  ;

           if (alpha[i] >= 65 && alpha[i] <= 90)
           {
               printf("\n %c", alpha   1);
           }
           else if (alpha[i] >= 97 && alpha[i] <= 122)
           {
               printf("\n %c", alpha   1);
           }
           else if (alpha[i] == 90)
           {
               printf("\n %c", 65);
           }
           else if (alpha[i] == 122)
           {
               printf("\n %c", 122);
           }
           else
           {
               printf("\n %c", alpha);
           }
       }
   }

   getch();

   return 0;
}

CodePudding user response:

Your test for upper case works for the ASCII encoding, but your changing the case of the character is incorrect: printf("\n %c", alpha 1); passes a pointer to the second character in the array. You should instead write:

    printf("\n %c", alpha[i]   1);

You should also handle non upper case characters, which are ignored as there is no else clause in the initial test.

You need to special case 'Z' but your test fails because 'Z' is already handled by the test (alpha[i] >= 65 && alpha[i] <= 90). Using hardcoded ASCII values is hard to read and non portable.

The other tests always fail as they are nested in test first test for uppercase characters.

Also note you should not use gets(), which cannot be used safely and has been removed from recent versions of the C Standard. Use fgets() instead.

Here is a modified version:

#include <stdio.h>

int main() {
    char alpha[100];

    printf("Enter String: ");
    if (fgets(alpha, sizeof alpha, stdin)) {
        for (int i = 0; alpha[i] != '\0'; i  ) {
            if (alpha[i] >= 'A' && alpha[i] < 'Z') {
                alpha[i]  = 1;
            } else
            if (alpha[i] == 'Z') {
                alpha[i] = 'A';
            }
        }
        fputs(alpha, stdout);
    }
    getchar();

    return 0;
}

The above code assumes that the uppercase letters are encoded as a contiguous block. This is the case of ASCII used almost everywhere today. Below is a portable version that can handle other character sets:

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

int main() {
    char alpha[100];
    const char *upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZA";

    printf("Enter String: ");
    if (fgets(alpha, sizeof alpha, stdin)) {
        for (int i = 0; alpha[i] != '\0'; i  ) {
            const char *p = strchr(upper, alpha[i]);
            if (p != NULL) {
                alpha[i] = p[1];
            }
        }
        fputs(alpha, stdout);
    }
    getchar();

    return 0;
}

CodePudding user response:

You should put the functionality in a function, thus giving it a name, and it then isolates the assignment from the rest of the environment.

Take e.g. this:

#include <assert.h>
#include <ctype.h>

void nextUpperCase(char *str) {
    assert(str && "function `nextUpperCase' not designed to handle null pointers");
    for (; *str != '\0';   str)
        if (isupper(*str) && !isalpha(  *str))
            *str = 'A';
}

It can then be easily tested without manual input:

// #include <locale.h> Needed for locale other than "C"
#include <stdio.h>

int main() {
    // setlocale(LC_ALL, "C");  // this is implicitly done before main is entered
    char buf[] = "Hello, Zee World!";
    puts(buf);  // prints `Hello, Z World!'
    nextUpperCase(buf);
    puts(buf);  // prints `Iello, A Xorld!'
    nextUpperCase(NULL);  // assertion
    return 0;
}

This particular solution assumes the "C" locale. If any other locale is used this proposed solution to "next" assumes 'A'...'Z' with no holes. For other locales the "next" defintion will need something very different.

CodePudding user response:

Try this code:

 #include <stdio.h>
#include <stdlib.h>
#include <conio.h>

int main()
{
   char alpha[100];

   printf("Enter String: ");
   gets(alpha);

   for (int i = 0; alpha[i] != 0; i  )
   {
       if (alpha[i] >= 'A' && alpha[i] <= 'Z')
       {
           //upperL  ;

           if (alpha[i] >= 65 && alpha[i] <= 90)
           {
               printf("\n %c", alpha   1);
           }
           else if (alpha[i] >= 97 && alpha[i] <= 122)
           {
               printf("\n %c", alpha   1);
           }
           else if (alpha[i] == 90)
           {
               printf("\n %c", 65);
           }
           else if (alpha[i] == 122)
           {
               printf("\n %c", 122);
           }
           else
           {
               printf("\n %c", alpha);
           }
       }
   }

   getch();

   return 0;
}
  • Related