简体   繁体   中英

C: String functions without string.h library

I have homework to do and I need some help. I didn't come here to get someone to do my work, just to help me.

I have to create my own string.h "library" (actually header and function file), so I am forbidden to use #include <string.h> and #include <ctypes.h> . They also recommended us not to use malloc, but it was just a recommendation, not forbidden. For most of the functions I know how to write them. I planned to save "strings" like arrays of chars like:

char array[50];

But I came to a problem of creating toupper and tolower functions. Sure, I can make huge switch cases or a lot if (else if's) 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 any better solution?

Strings are going to be created randomly so they will look somewhat like this:

ThisISSomESTRing123.

So after toupper function, randomly generated string should look like this:

THISISSOMESTRING123.

Also how would you create puts function (printing) so everything would be in same row? Just "printf" inside "for" loop?

Your system probably uses ASCII. In ASCII, the codepoints of lowercase characters and uppercase characters are sequential.

So we can just do:

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

Other character encodings can become much more complicated . For example, EBCDIC doesn't have contiguous characters. And UTF-8 introduces a world of problems because of the numerous languages that you need to support.

You can just check if the character is within the range of characters, say between 'a' and 'z' and just add or subtract 32, which is the difference between the ascii value of capital and lower letters. See the ascii table here: http://www.asciitable.com/

Just do

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

It will convert lower case to upper case by changing a..z to 0..26 then adding the ASCII value for 'A' to it.

There's a neat trick for converting ASCII cases. Consider these characters:

A - binary 01000001
a - binary 01100001


As we can see, the difference is in the 6th bit, counting from the right. Indeed, the difference between uppercase and lowercase ASCII chars is 2^5 = 32. So, to convert a letter to uppercase, simply AND it with 0xDF (11011111) to set that bit to 0. In this way you don't even have to check if the character is in uppercase already.

Note that this will break non-letter characters that are above 0x60 , namely the backtick, { , | , } and ~ . But as long as you don't have these in your strings, it should be fine to use this and you can avoid an if :).

Note: Only use that as a cool trick for this homework. Normally you should just use proper, tested solutions (aka string.h ).

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

use this function to make all the letters uppercase in a string just call the toUpper funcn and give your array as a prarameter. for printing the array just use for loop and move in array index elements and print the letters.

   for(int i=0;arr[i] !='\0';i++)
   {
    printf("%c",arr[i]);
   } 

this will print every elements in the array which is the string,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM