Implementation of ToLower function in C

前端 未结 4 1089
不知归路
不知归路 2021-01-27 08:28

I am writing my own implementation of ToLower(char *str) in C. But i am getting segmentation fault in the function. The function which i wrote is :

void ToLower(         


        
相关标签:
4条回答
  • 2021-01-27 09:01

    It's generally considered to be good form to accept a length parameter in functions that operate on strings. This way, if you pass in a string that's not null-terminated, the function won't loop past the end of the input.

    You could step through the function call with a debugger, or add a print statement in the loop and see how many times it's iterating.

    0 讨论(0)
  • 2021-01-27 09:12

    Although the name of your function ToLower() suggests you are re-writing the ANSI C version of tolower(), (i.e. changing one char from upper to lower case) your implementation suggests you really want an entire string to be changed. Perhaps the name StrToLower() is what you are really intending? (i.e. change the entire string). If that is the case, the following code illustrates. (if you really want to re-write tolower(), it should really be a different question, with a prototype similar to the C version, and changing only one char per call)

    This answer assumes because of the tag "c" in your question, that you are not wanting the .NET version of String.ToLower() (which DOES convert a string). If this is a wrong assumption, disregard my rants.

    This method will work with a char *str="STRING";, or with a constant string ("STRING") as an argument.
    [Expanded] Include an implementation of ToLower, in case that is what OP really wanted.

    #include <stdio.h>
    char * StrToLower(char *str) ;
    int toLower(int chr);
    
    int main(void)
    {
        char lowered[] = "UPPER to Lower"; 
    
        sprintf(lowered, "%s",StrToLower(lowered));
        printf("%s\n", lowered); //works with a variable buffer argument
    
        lowered[0]=0;//clear the buffer
    
        sprintf(lowered, "%s",StrToLower("UPPER to Lower")); 
        printf("%s\n", lowered); //also works with a literal string argument
    
        getchar();//view results
        return 0;
    }
    
    char * StrToLower(char *str)
    {
        char *pNew1 = str;
        char *pNew2 = str;
    
        if(str != NULL) //NULL ?
        {
            if(strlen(str) != 0) //"" ?
            {
                while(*pNew1)
                {
                    *pNew2 = toLower(*pNew1); 
                    ++pNew2;
                    ++pNew1;
                }
                *pNew2 = '\0';
                return str;// return changed string
            }              // and prevent returning null to caller
        }
        return "";//Will never get here for non-null input argurment
    }   
    
    int toLower(int chr)//touches only one character per call
    {
        return (chr >='A' && chr<='Z') ? (chr + 32) : (chr);    
    }
    

    Results: (answering Roland's comment)
    enter image description here

    0 讨论(0)
  • 2021-01-27 09:13

    for safe variable you can use the following prototype:

    void ToLower(const char *str)
    
    0 讨论(0)
  • 2021-01-27 09:24

    You are almost certainly failing when you call it like:

    int main(void)
    {
        ToLower("HelloWorld");
        return 0;
    }
    

    This is because "HelloWorld" is a literal, constant string, and you cannot change its contents.

    Try instead:

    int main(void)
    {
        char str[] = "HelloWorld";
    
        // Now str is your own local buffer, that you can modify.
        // It is initialized with the text, but that text can be changed.
        ToLower(str);
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题