C: String functions without library string.h

I have homework and I need help. I did not come here to get someone to do my job, only to help me.

I need to create my own library string.h "(the actual header and function file), so I am forbidden to use and , They also recommended that we not use malloc, but it was just a recommendation, and not forbidden. For most functions, I know how they I planned to save "strings", for example arrays of characters, for example: #include <string.h>#include <ctypes.h>

char array[50];

But I ran into the problem of creating toupper and tolower functions. Of course, I can do huge switching cases or a lot if (otherwise, if) like this:

if(string[i]=='a') {string[i]=='A' };
 else if(string[i]=='b') {string[i]=='B' };
    .
    .
    .
    else if(string[i]=='z') {string[i]=='Z' };

But is there a better solution?

, :

ThisISSomESTRing123.

, toupper , :

ThisISSomESTRing123.

puts (), ? "printf" "for"?

+4
4

, , ASCII. ASCII .

, :

void to_upper(char *message) {
    while (*message) {
        if (*message >= 'a' && *message <= 'z')
            *message = *message - 'a' + 'A';
        message++;
    }   
}   

. , EBCDIC . UTF-8 - , .

+7

, , "a" "z" 32, ascii . . ascii : http://www.asciitable.com/

+3

if (string[i] >= 'a' && string[i] <= 'z') {
    string[i] = string[i] - 'a' + 'A';
}

, a..z 0..26, ASCII "A".

+3

ASCII-. :

A - 01000001
A - 01100001


, 6- , . , ASCII 2 ^ 5 = 32. , , 0xDF (11011111) 0. , , .

Note that this will break non-alphanumeric characters that are above 0x60, namely the flip side, {, |, }and ~. But as long as you don't have them in your lines, it should be nice to use this, and you can avoid if:).

Note. Use it only as a trick for this homework. Usually you should just use the correct proven solutions (aka string.h).

+2
source

Source: https://habr.com/ru/post/1589074/


All Articles