0

I'm just beginning in C and I'm having a hard time with a segmentation fault in my code(which is pretty simple btw). The goal of the exercice is to capitalize each letter in a string. Could someone please help me out ?

here is the assignment: "Write a function that puts every letter of every word in it in uppercase" (we've got to do it manually without using other libraries methods).

here is the error message: "[1] 14328 segmentation fault (core dumped) ./a.out"

#include <stdio.h>
#include <unistd.h>

char *my_strupcase(char *str) {

    int index = 0;

    for (index = 0; str[index] != '\0'; index++) {
        if ((str[index] >= 'a' && str[index] <= 'z') || (str[index] >= 'A' && str[index] <= 'Z')) {
            str[index] = (str[index] - 32);
            printf("%c \n", str[index]);
        } 
    }

    return str;
}

int main() {
    my_strupcase("salut");
    return (0);
}
Unknown_
  • 122
  • 2
  • 7
  • 3
    `"salut"` is a sting literal and should not be attempted to be written to. – Eugene Sh. Oct 18 '19 at 15:05
  • 1
    Possible duplicate of [String literals: pointer vs. char array](https://stackoverflow.com/questions/12795850/string-literals-pointer-vs-char-array) – gstukelj Oct 18 '19 at 15:06
  • 3
    Why do you need to check for the range A-Z? They are already capitalized. – jmq Oct 18 '19 at 15:06
  • 2
    You don't need `` for the code shown. – pmg Oct 18 '19 at 15:07
  • 2
    You must decide what your function do. Now you are making uppercase from lowercase letters and uppercase letters into some kind of characters that are not letters. – Skiv Hisink Oct 18 '19 at 15:12
  • 1
    You can use library function `toupper()` in `ctype.h` to convert to upper case. You don't have to check if they are appropriate characters first. But if you want to, that's what `islower()` is for. – Weather Vane Oct 18 '19 at 15:13
  • @pmg its one of the many restrictions on the exercise haha :( – Unknown_ Oct 18 '19 at 15:23
  • 1
    @Unknown_ questions about an exercise should include any restrictions. – Jabberwocky Oct 18 '19 at 15:25
  • 1
    @Unknown_: the tag `visual-studio` and the restriction of using `` seem contradictory!! – pmg Oct 18 '19 at 15:27
  • @pmg not sure if using `unistd.h` is part of the restrictions. – Jabberwocky Oct 18 '19 at 15:31
  • [Here's `` in all its glory](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/unistd.h.html) – pmg Oct 18 '19 at 15:32

1 Answers1

3

This is a corrected version with comments:

#include <stdio.h>
                                                        // you don't need unistd.h 
char *my_strupcase(char *str) {
    for (int index = 0; str[index] != '\0'; index++) {  // declare index here 
                                                        // as it's not used anywhere else
        if ((str[index] >= 'a' && str[index] <= 'z') {  // only process a-z
            str[index] = str[index] - ('a' - 'A');      // 'a' - 'A' is more clear then 32
            printf("%c \n", str[index]);
        } 
    }

    return str;
}

int main() {
    char mytext[] = "salut HELLO 123";   // here mytext is an array of char
    my_strupcase(mytext);                // that can be modified.
    puts(mytext);
    return (0);
}

Output should be:

SALUT HELLO 123

Your program segfaults because you try to modify a string literal; this is undefined behaviour and results in a program crash on many platforms. You can find more detailed information about this in following SO article.

Be aware that capitalisation should normally be done using standard functions such as toupper.

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • @VladfromMoscow this answers the question, and shows the other problems. – Jabberwocky Oct 18 '19 at 15:19
  • To me `'a' - 'A'` invites `'A' - 'a'`. The most clear value is `32 /* difference in ASCII between lowercase and corresponding uppercase letters */` (maybe using a define) – pmg Oct 18 '19 at 15:22