0

I think my program is right but its showing an error "Segmentation fault(core dumped)". What is the problem?

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

int main(void)
{
    string s="I am a good boy";
    int len=strlen(s);
    int word=0;
    for(int i=0;i<len;i++)
    {
        s[i]=toupper(s[i]);
        if(s[i]>=65 && s[i]<=90)
        {
            word++;
        }
    }
    printf("%i",word);
}

expected output:

11

actual output:

segmentation fault(core dumped)

EDIT: It seems using char[] works. On another note, somehow, the above code works fine when it is on another function, i.e not int main(void).

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
supertamp
  • 1
  • 1
  • 1
    The problem is CS-50 being a bad class fooling you into thinking C got a string class. I'll link a duplicate explaining why. I would strong recommend abandoning any teaching material related to CS-50 or Harvard. – Lundin Jun 22 '23 at 07:30
  • A "segmentation fault" is a crash, that commonly comes from using memory in a way you're not supposed to. Like trying to modify a literal string, which is *not* allowed. Use an *array* instead of a pointer. – Some programmer dude Jun 22 '23 at 07:31
  • Also please don't use [*magic numbers*](https://en.wikipedia.org/wiki/Magic_number_(programming)). If the number `65` is supposed to be the ASCII code for `'A'` then use the actual character constant instead. Or even better, use the standard [`isupper`](https://en.cppreference.com/w/c/string/byte/isupper) function instead. – Some programmer dude Jun 22 '23 at 07:33
  • Actually, for your program there's no need for the modification of the string, just use [`isalpha`](https://en.cppreference.com/w/c/string/byte/isalpha) which checks if a character is a letter (upper or lower case). – Some programmer dude Jun 22 '23 at 07:35
  • 1
    I'm afraid that you will not be able to understand the duplicate question, because you will only learn in [week 4 of CS50](https://cs50.harvard.edu/x/2023/weeks/4/) what a `char *` is and what its relationship to the `string` data type is. – Andreas Wenzel Jun 22 '23 at 07:36
  • Until you reach week 4 of CS50, you should know the following: A variable of type `string` does not actually contain a sequence of characters. It is merely a reference to an already existing `char` array. Some of these referenced `char` arrays are writable, for example those returned by `get_string` or the one referenced by `argv[1]`, `argv[2]`, etc. However, the expression `"I am a good boy"` will give you a reference to a read-only array with this content. Therefore, when your `string s` references this read-only array, you will get a segmentation fault when you attempt to write to it. – Andreas Wenzel Jun 22 '23 at 07:46
  • If you want a `char` array that is writable, then I suggest that you simply define a `char` array, by changing the line `string s="I am a good boy";` to `char s[] = "I am a good boy";`. That line is equivalent to `char s[16] = "I am a good boy";` In other words, if you do not specify a size, the compiler will automatically calculate the necessary size for storing all characters in the array, including the terminating null character. – Andreas Wenzel Jun 22 '23 at 07:59
  • In contrast to what someone else has written, I do recommend that you stay with the CS50 course. Even if the data type `string` may be confusing for you, all of this will be explained in [week 4 of CS50](https://cs50.harvard.edu/x/2023/weeks/4/). – Andreas Wenzel Jun 22 '23 at 08:06
  • 1
    @AndreasWenzel A beginner class which still has not gotten to pointers after 4 weeks can by definition not be a good one. After 4 weeks I would have expected a decently paced programming class to have covered pretty much _all_ of the language basics, give or take. The reason why CS50 is that slow is because it is mixing in algorithms before students even have beginner-level programming knowledge - a very bad idea. Better universities teach a programming language first for some 4 weeks, then algorithms as a separate follow-up class for some 4 weeks more. – Lundin Jun 22 '23 at 12:46

0 Answers0