-1
int count_letters(string text, int length);
int count_words(string text);
int count_sentences(string text);

void final(int letters, int words, int sentences);

int main(void)
{
    string text = get_string("Text: \n");
    int length = strlen(text);
   //printf("%i\n",length);

    int letters = count_letters(text, length);

Here I need variable "length" in all these four functions but all these functions already have a string type parameter.Is it possible to pass different types of parameters in a function?

Basically I want to know if this is correct (line 1 and line 13) and if no then how can i use this length variable in all these functions without having to locally define it in each function?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
smbhav
  • 1
  • 5
  • I'm not quite sure what you're asking here. You should [edit] and show an example of what you mean with _need variable "length" in all these four functions but all these functions already have a string type parameter._ If you need an `int length` paramater in `count_words`, you need to put it there yourself.... like `int count_words(string text, int length) ...`. – Jabberwocky Nov 25 '22 at 19:04
  • First and last line of above code is valid then ? – smbhav Nov 25 '22 at 19:10
  • But yes, the declaration `int count_letters(string text, int length);` and `int letters = count_letters(text, length);` are both correct. Your learning material should explain this. – Jabberwocky Nov 25 '22 at 19:12
  • Okay that’s all i needed to know. Thanks – smbhav Nov 25 '22 at 19:13
  • 1
    You do not need to pass length if the string references a valid C string. – 0___________ Nov 25 '22 at 19:16
  • 1
    Is `string` the actual name of the data type you are using? Are you using the CS50 header file `cs50.h`? – Andreas Wenzel Nov 25 '22 at 20:21

2 Answers2

2

C strings are null character terminated. You do not need to pass the length of the string to the function. You need to iterate until you reach this character

Example:

int count_letters(string text)  //better to return size_t
{
    int result = 0;
    for(int index = 0; text[index] != '\0'; index++) 
    {
        if(isalpha((unsigned char)text[index]))
        {
            result += 1;
        }    
    }
    return result;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • This works. Nice trick. I Didn’t know . Thanks – smbhav Nov 25 '22 at 19:30
  • Can i ask you something ? here- `if(isalpha((unsigned char)text[index]))` why did you use unsigned char when it works without it . What's its importance ? – smbhav Nov 25 '22 at 20:22
  • I really wonder sometimes what they teach people in these CS50 courses.... – Jabberwocky Nov 25 '22 at 20:26
  • i am only at week 2. – smbhav Nov 25 '22 at 20:27
  • @smbhav: [This answer](https://stackoverflow.com/a/45007070/12149471) to a C++ question explains why the cast to `unsigned char` is necessary with `isdigit`. For the same reason, it is also necessary in C, and also with the function `isalpha`. The cast is only necessary for character codes above `127`, so you don't need it for character codes below that number. – Andreas Wenzel Nov 25 '22 at 20:31
  • @smbhav: If you feel confused by the cast to `unsigned char`, I suggest that you leave out that cast for now. The CS50 teacher David Malan deliberately does not use the cast, in order to not confuse students. Since you are probably only using character codes below `127` for now, it is unlikely that you will run into any issues by leaving out the cast. The cast will only become important when you start using character codes larger than `127`. – Andreas Wenzel Nov 25 '22 at 20:39
  • @AndreasWenzel Thanks for your time. I read that answer throughly but can’t say i manage to understand all of it . But atleast i got some vague idea why its important. I think you are right, David left this out for now like many other topics that are not in scope for initial weeks. I am sure moving forward into the later weeks I’ll learn about this. – smbhav Nov 25 '22 at 20:53
  • @smbhav: In short, on most platforms, a `char` is equivalent to a `signed char`, which can represent values between `-128` to `+127`. However, on some platforms, a `char` is equivalent to an `unsigned char`, which can represent values between `0` to `+255`. The functions `isalpha`, `isdigit`, etc. expect values in the range `0` to `+255`, i.e. in the range of an `unsigned char.` If you do not cast to an `unsigned char` and pass a `char` with an ASCII code above `127`, then, depending on the platform, the value may be negative, which some implementations of `isalpha` cannot handle. – Andreas Wenzel Nov 25 '22 at 21:15
  • @smbhav: Another issue is that the macro constant `EOF`, which is returned by several functions in the case of end-of-file or error (e.g. [getchar](https://en.cppreference.com/w/c/io/getchar)), is a negative value (`-1` on most platforms). Therefore, if you are passing character codes that have a negative value (i.e. character codes above `127` stored in a `char` on platforms on which `char` is equivalent to `signed char`) to the function `isalpha`, then that function cannot distinguish the special value `EOF` from an actual character code. [continued in next comment] – Andreas Wenzel Nov 26 '22 at 14:26
  • @smbhav: [continued from previous comment] This ambiguity can be resolved by casting the character codes to `unsigned char`, so that they are in the range `0` to `255`, and therefore never have a negative value that could be equal to `EOF`. However, as previously stated, all of this is not an issue as long as you are not using character codes above `127`, and the CS50 course never asks you do to that, as all important character codes for the English language have values below `127`. – Andreas Wenzel Nov 26 '22 at 14:32
1

Of course it's possible. You already did it:

int count_letters(string text, int length);

count_letters has a string parameter called text and an int parameter called length.

And I'm sure you already know some functions that allow this:

     printf("the magic number is %d\n", 42);
//   ^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^  ^^
// function      const char *           int
user253751
  • 57,427
  • 7
  • 48
  • 90