Finding Capital Letter

I've made a function that finds the capital or lowercase letter of a letter that was entered, but I find my function too long, and I wonder if there is any shorter and easier way to do this than I did.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
char hittaStorLitenBokstav(char test){
    char s;
    switch(test){
        case 'a':
            s = 'A';
            break;
        case 'b':
            s = 'B';
            break;
        case 'c':
            s = 'C';
            break;
        case 'd':
            s = 'D';
            break;
        case 'e':
            s = 'E';
            break;
        case 'f':
            s = 'F';
            break;
        case 'g':
            s = 'G';
            break;
        case 'h':
            s = 'H';
            break;
        case 'i':
            s = 'I';
            break;
        case 'j':
            s = 'J';
            break;
        case 'k':
            s = 'K';
            break;
        case 'l':
            s = 'L';
            break;
        case 'm':
            s = 'M';
            break;
        case 'n':
            s = 'N';
            break;
        case 'o':
            s = 'O';
            break;
        case 'p':
            s = 'P';
            break;
        case 'q':
            s = 'Q';
            break;
        case 'r':
            s = 'R';
            break;
        case 's':
            s = 'S';
            break;
        case 't':
            s = 'T';
            break;
        case 'u':
            s = 'U';
            break;
        case 'v':
            s = 'V';
            break;
        case 'x':
            s = 'X';
            break;
        case 'y':
            s = 'Y';
            break;
        case 'z':
            s = 'Z';
            break;
        case 'A':
            s = 'a';
            break;
        case 'B':
            s = 'b';
            break;
        case 'C':
            s = 'c';
            break;
        case 'D':
            s = 'd';
            break;
        case 'E':
            s = 'e';
            break;
        case 'F':
            s = 'f';
            break;
        case 'G':
            s = 'g';
            break;
        case 'H':
            s = 'h';
            break;
        case 'I':
            s = 'i';
            break;
        case 'J':
            s = 'j';
            break;
        case 'K':
            s = 'k';
            break;
        case 'L':
            s = 'l';
            break;
        case 'M':
            s = 'm';
            break;
        case 'N':
            s = 'n';
            break;
        case 'O':
            s = 'o';
            break;
        case 'P':
            s = 'p';
            break;
        case 'Q':
            s = 'q';
            break;
        case 'R':
            s = 'r';
            break;
        case 'S':
            s = 's';
            break;
        case 'T':
            s = 't';
            break;
        case 'U':
            s = 'u';
            break;
        case 'V':
            s = 'v';
            break;
        case 'X':
            s = 'x';
            break;
        case 'Y':
            s = 'y';
            break;
        case 'Z':
            s = 'z';
    }
    return s;
}
Don't worry, I came up with a remarkably shorter one myself :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
char hittaStorLitenBokstav(char test){
    char s;
    char alphabet1[26] = "abcdefghjklmnopqrstuvwxyz";
    char alphabet2[26] = "ABCDEFGHJKLMNOPQRSTUVWXYZ";
    for(int x=0;x<26;x++){
        if(test == alphabet1[x]){
            s = alphabet2[x];
        }
        else if(test == alphabet2[x]){
            s = alphabet1[x];
        }
    }
    return s;
}
Last edited on
char alphabet1[26] = "abcdefghjklmnopqrstuvwxyz";

You'd need 27 characters. 26 for the alphabet + 1 for the null terminator.

Also, you are missing i / I.

Also, the output is undefined if 'test' is not an alphabetical character, since you don't initialize 's'


Anyway, here's another way to do it:

1
2
3
4
5
6
7
8
9
char hittaStorLitenBokstav(char test)
{
    if( (test >= 'A') && (test <= 'Z') )
        return test - 'A' + 'a';
    if( (test >= 'a') && (test <= 'z') )
        return test - 'a' + 'A';

    return test;
}
Another simple way to do it using for loops.

Code
1
2
3
4
5
6
7
char hittaStorLitenBokstav(char test)
{
     for (int jjj = 0; jjj < 26; ++jjj)
          if (test == 'a' + jjj)
               test = 'A' + jjj;
     return test;
}


Explanation
Loop will run 26 times. Each time adding 1 to jjj starting at zero and ending at 25.

'a' + 0 == 'a'
'A' + 0 == 'A'

'a' + 1 == 'b'
'A' + 1 == 'B'

'a' + 25 == 'z'
'A' + 25 == 'Z'


Read
http://www.cplusplus.com/doc/ascii/
http://www.cplusplus.com/doc/tutorial/control/
Last edited on
And another way is to use the toupper function - not sure whether you are allowed to use this, or were unaware of it.

http://www.cplusplus.com/reference/cctype/toupper/


This is in addition to Disch's post which is great for showing the OP how to make use of the char values.

HTH

Edit: there are 2 other versions of this function - which you can find in the reference. The 1 quoted is the C version. Here is a C++ one :

http://www.cplusplus.com/reference/locale/toupper/
Last edited on
@ Disch

I don't really get how your function works, but it certainly does.

@ TheIdeasMan

Actually I didn't know about it :)
@goran
@ Disch

I don't really get how your function works, but it certainly does


it works because each character has a value associated with it, so 'a' = 97, 'b' = 98, and so on.

capitals have values with them as well, 'A' = 65, 'B' = 66, and so on.

his code checks whether the input is a capital or not and then uses the values of the characters to do the conversion

so "C" for example

'C' - 'A' + 'a' = 'c' . . . which is the same as 67 - 65 + 97 = 99 or 'c'

i hope this helps explain whats going on there :D

1
2
3
char test = 'a';
int teste = test;
std::cout << teste;


you can use that to look at character values if you want

EDIT:spelling
Last edited on
Now I get it :) Thank you for your explanation.
Topic archived. No new replies allowed.