2

I'm making program that finds length of string entered by user. Everything is working but program also counts spaces. So, how to find length of string without counting spaces?

sefisko
  • 35
  • 4
  • 4
    Add an `if` to ignore the spaces. Is this homework? – the busybee Dec 10 '21 at 10:49
  • 2
    Something like `if (*c != ' ') count++;`? I'm sure you can do it. – Jabberwocky Dec 10 '21 at 10:52
  • There's [`isspace`](https://en.cppreference.com/w/c/string/byte/isspace) in `ctype.h` header catching *any* whitespace including tabulator, carriage return, newline and others. – Aconcagua Dec 10 '21 at 10:54
  • Alternative to `if(...) ++count;` is `count += *c != ' ';`, by the way. – Aconcagua Dec 10 '21 at 10:56
  • What about punctuation characters? Would you want to exclude these as well? `count += isalnum((unsigned char)*c);`, including digits, or `isalpha` without digits – note: the cast to unsigned is necessary for the case your strings contains symbols with numeric values > 127, as `char` potentially is signed (for `isspace` alike!). – Aconcagua Dec 10 '21 at 10:59
  • @Aconcagua `count += *c != ' ';` is pretty hacky IMO. – Jabberwocky Dec 10 '21 at 11:00
  • @Jabberwocky Why? Comparisons being *guaranteed* to result in either 0 or 1 makes that pretty self-explanatory in my eyes, and fully portable as well – would we need to assume lack of such knowledge? – Aconcagua Dec 10 '21 at 11:09
  • 2
    @Aconcagua if you want make code harder to read you can ` for(; *c; c++, length += *c != ' ');` or `while(length += *c != ' ', *(++c));` – 0___________ Dec 10 '21 at 11:09
  • @Aconcagua separate line makes code reading easier – 0___________ Dec 10 '21 at 11:11
  • @Aconcagua sorry, I wasn't clear enough. Your code is perfectly correct and portable, but not very readable (at least for beginners). – Jabberwocky Dec 10 '21 at 11:21
  • @0___________ Well, with that argument `if(*c != ' ') length++;` in one single line as in your answer is just as bad – I'd qualify even worse, as at a very first glance body of the `if` appears to be lacking... – Aconcagua Dec 10 '21 at 11:24
  • @Aconcagua short single statement `if` bodies are done this way for years – 0___________ Dec 10 '21 at 11:26
  • @0___________ And is *not* accepted by so many coding guide lines, including those widespread ones like MISRA (that actually even mandate placing braces). – Aconcagua Dec 10 '21 at 11:27
  • @0___________ For your early comment: Question is now where we cross the frontier to the territory of exaggeration... – Aconcagua Dec 10 '21 at 11:30
  • @Aconcagua I do not think that he will start work soon as a programmer in the automotive industry – 0___________ Dec 10 '21 at 14:00

1 Answers1

2
  1. Use the correct types for sizes
  2. *(p + x) === p[x] - where p is a pointer and x has integral type. Your both function are exactly the same.
  3. Use names that have some meaning string_array is not too informative and definitely does not indicate that it is returning the length of the string without spaces.
  4. do not use gets functions as it is very dangerous. Use functions that limit the number of characters placed in the string. For example fgets
size_t strlenNoSpaces(const char * restrict c)
{
    size_t length = 0;
    for(; *c; c++)
    {
        if(*c != ' ') length++;
    }
    return length;
}

int main()
{
    char string[30];
    size_t length, i;

    printf("Enter string: ");
    fgets(string, 29, stdin);

    length = strlen(string);
    printf("\nLength of string using strlen.\n");
    printf("Length of string %zu.\n", length);

    length = strlenNoSpaces(string);
    printf("\nLength of string not counting the spaces.\n");
    printf("Length of string %zu.\n", length);

    return 0;
}
0___________
  • 60,014
  • 4
  • 34
  • 74
  • @sefisko: Note that `fgets` requires a limit of one less than available array length as the limit refers to the numbers of characters actually read, but input still gets null-terminated afterwards (thus 30 <-> 29). – Aconcagua Dec 10 '21 at 11:14
  • And `sizeof(string) - 1` would be better than `29` here, too. And I personally avoid calling variables `string` in C code - it's a keyword in far to many other languages. And imagine some poor noob pastes this code into some CS50 problem - the variable name `string` itself the collides with CS50's abomination of a `typedef`... – Andrew Henle Dec 10 '21 at 12:05