Algorithm

Hi guys im not very sure how to write the algorithm for this code
Thank you in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdio.h>
#include <ctype.h> // toupper(), tolower(), isupper(), islower()

//This function will return the ASCII value of the character
int get_ascii(char c)
{
    return (int)c; //Need to cast the char as an int to get the ASCII value
}

int main()
{
    char ch; //To hold the character

    //Loop till ch equals '#'
    do
    {
        printf("Enter a character: ");
        scanf(" %c", &ch); //Get the character from the user, notice the space before %c
                           //The space tells to ignore the whitespace characters like '\n'
        
        int ascii_value = get_ascii(ch); //Get the ascii value of the character
        printf("\nASCII value of %c: %d\n\n", ch, ascii_value); //Display the ASCII value
        
        if(islower(ascii_value)) //If the character is a lowercase character (a..z)
        {
            char upper_ch = (char)toupper(ascii_value); //Get the uppercase character
            int upper_ascii = get_ascii(upper_ch); //Get the ASCII value of the uppercase character

            //Display the uppercase character and its ASCII value
            printf("Uppercase version of %c is %c.\n", ch, upper_ch);
            printf("ASCII value of %c: %d\n\n", upper_ch, upper_ascii);
        }
        else if(isupper(ascii_value)) //If the character is an uppercase character (A..Z)
        {
            char lower_ch = (char)tolower(ascii_value); //Get the lowercase character
            int lower_ascii = get_ascii(lower_ch); //Get the ASCII value of the lowercase character

            //Display the lowercase character and its ASCII value
            printf("Lowercase version of %c is %c.\n", ch, lower_ch);
            printf("ASCII value of %c: %d\n\n", lower_ch, lower_ascii);
        }
    } while(!(ch == '#'));

    printf("Have a nice day peep! :D\n");

    return 0;
}


Last edited on
If you are restricted to the English alphabet, the logic is:
- get ‘c’ from the user
- if ‘c’ ranges from 'A' to 'Z', c → -'A' + 'a'
- if ‘c’ ranges from 'a' to 'z', c → -'a' + 'A'

Sorry, can’t write it C.
(Btw, don’t you think you’d get better help in a C forum?)
Topic archived. No new replies allowed.