Nishant Kumar
Nishant Kumar

Reputation: 21

Implementation of ToLower function in C

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(char *str)
{
    while(*str != '\0')
    {
        if(*str >=65 && *str<=90)
        {
            // It fails in the below assignment
            *str = *str + 32;
        }
        str++;
    }

}

Upvotes: 1

Views: 12086

Answers (4)

ryyker
ryyker

Reputation: 23226

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

Upvotes: 0

PersianGulf
PersianGulf

Reputation: 2935

for safe variable you can use the following prototype:

void ToLower(const char *str)

Upvotes: -2

abelenky
abelenky

Reputation: 64710

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;
}

Upvotes: 12

Zach Rattner
Zach Rattner

Reputation: 21363

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.

Upvotes: 5

Related Questions