0

I'm trying to fix my program...but I don't know how to fix this error "Bus error: 10".

convert to lower case function:

void toLowerCase(char* s){
    while(*s){
        if(*s >= 'A' && *s <= 'Z'){
            *s = tolower(*s);
            ++s;
        }
    }
}
Nam
  • 33
  • 5
  • 1
    Have you tried to debug to see what is the input of that function when it is called ? – dim mik Nov 25 '19 at 17:01
  • 8
    Your `while` loop will get stuck and run forever as soon as it comes across a character that isn't an uppercase letter! Move the `++s` to ***outside*** the `if` block. – Adrian Mole Nov 25 '19 at 17:01
  • 4
    You're less likely to make that kind of mistake if you use a `for` loop instead of `while`. – Barmar Nov 25 '19 at 17:02
  • 3
    Note that `tolower` also checks that it was passed an uppercase character before converting it, and does not know you already made that check. So you are wasting processor time. – Weather Vane Nov 25 '19 at 17:03
  • 1
    @Bamar, so we will see that horrible `for (int i=0; i – Paul Ogilvie Nov 25 '19 at 17:05
  • Either 1) You don't need the if-statement or 2) you shouldn't call `tolower` but do the conversion yourself. – Support Ukraine Nov 25 '19 at 17:05
  • 3
    @4386427 you *should* use the supplied function: it is safe and portable. – Weather Vane Nov 25 '19 at 17:06
  • @WeatherVane sure, sure ... but I guess this is yet another homework and it's like the goal is to write your own conversion... – Support Ukraine Nov 25 '19 at 17:07
  • 2
    @PaulOgilvie `for(; *s; ++s) { }` – I cannot understand why people always go over indices when they could do far better with iterators... – Aconcagua Nov 25 '19 at 17:08
  • 2
    @PaulOgilvie: I certainly hope not, but `for(; *s; ++s)` would be an improvement. – Fred Larson Nov 25 '19 at 17:08
  • Does this answer your question? [Why Can I not modify a string literal in c?](https://stackoverflow.com/questions/58584310/why-can-i-not-modify-a-string-literal-in-c) – 0___________ Nov 25 '19 at 17:25
  • @P__J__ not really :/ but I solved it now – Nam Nov 25 '19 at 17:39

2 Answers2

0

Thank you for your answers guys :D that's my code:

void toLowerCase(char* s){
    while(*s){
        *s = tolower(*s);
        ++s;
    }
}

Now I call the function with:

char* arr = "PrOgRaM";
toLowerCase(arr);
Nam
  • 33
  • 5
  • Yep, that is a literal string and is unmodifiable (read-only). Any attemp to change a letter to lowercase will cause a seg fault or other type of program abort. – Paul Ogilvie Nov 25 '19 at 17:22
  • Instead, use `char arr[] = "PrOgRaM";` – Paul Ogilvie Nov 25 '19 at 17:23
  • yeah that's the case...I thinking of duplicating the char array and modify it then. But the Problem is I shouldn't use the function strdup() or any function which allocates memory... – Nam Nov 25 '19 at 17:26
  • @PaulOgilvie actually it's undefined behaviour, so it may work on certain platforms. – Jabberwocky Nov 26 '19 at 07:55
-1

https://godbolt.org/z/cjf3kS

#include <stdio.h>
#include <ctype.h>

char *mylower(char *str)
{
    char *bkp = str;
    while(*str = tolower(*str)) str++;
    return bkp;
}


int main()
{
    char str[]="sdfh><:{^$#WGFDS@SDVERTY3453gflsdwjlgkerZXC||A||";

    printf("%s\n", mylower(str));
}
0___________
  • 60,014
  • 4
  • 34
  • 74