0

Currently I have websites store in char *banned[100] and I want to convert them all into lowercase using:

char *banned[100];
int x = 0;
if(fgets(temp, 100, file) != NULL) {
        char *tempstore;
        tempstore = (char*) malloc(sizeof(temp));
        strcpy(tempstore, temp);
        banned[x] = tempstore;
        x++;
    }
char temps[100];        
while(banned[c]){
        temps[c]=putchar(tolower(*banned[c]));
        c++;
}

But the results are not what I'm expecting. Can I get some tips/hints on what I'm doing wrong?

2
  • 6
    What is putchar() doing there? What do you think it does? Also note that banned is an array of 100 char* (string) elements, while temps is an array of 100 char (character) elements. temps is a string by itself; it is not an array of strings. Commented Nov 21, 2014 at 4:55
  • Ohh I wasnt aware of that.. Thanks for the telling me! I'm still very new to C :P Commented Nov 21, 2014 at 5:04

3 Answers 3

4

Since you know the size of the string (char array), you can just use a for loop.

char temps[100];
size_t i;

for(i = 0; i < 100; i++)
  temps[i] = tolower(temps[i]);
Sign up to request clarification or add additional context in comments.

5 Comments

The if conditional is redundant; tolower() will perform this same test.
My values are stored in char *banned[100] so that wouldn't work in my case.
@self Yes. Redundant code is bad. It provides zero benefit while giving bugs a place to hide.
It's bad idea to convert all chars in temps. You have stop at the last '\0' char.
Guys, it would be better if you actually update the answer with good suggestions. Most people will never read the comments here.
1

Use the below code to meet your requirement,

char *strtolower(char *s)
{
    char *d = (char *)malloc(strlen(s));
    while (*s)
    {
        *d =tolower(*s);
        d++;
        s++;
    }
    return d;
}

int main(void)
{
    char *banned[100];
    char *temps[100];
    char temp[100];

    FILE *file = fopen("test.txt", "r");
    int x = 0;

    if (file != NULL)
    {
        while(fgets(temp, 100, file) != NULL)
        {
            char *tempstore;
            tempstore = (char *) malloc(sizeof(temp));
            strcpy(tempstore, temp);
            banned[x] = tempstore;
            x++;

            puts(tempstore);

        }

        int c = 0;

        while(c < x)
        {
            temps[c] = strtolower(banned[c]);
            puts(temps[c]);
            c++;
        }
    }
    return 0;
}

14 Comments

That would break another piece of my code where I store values into banned:
what break ? also you need to confirm the string banned is null terminated. That means, the end of string must be marked with '\0'.
Oh sorry, I meant if I changed char *banned[100] to char banned[100], another part of the code will break. I've updated the question with more code. Sorry about the confusion!
you trying to read single entry in file ? or can be many entries ?
The file can contain up to 100 website urls, which would each be on a different line
|
1

You can try this :

char temps[100];
size_t i;

for(i = 0; temps[i] != '\0'; i++)
  temps[i] = tolower(temps[i]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.