How to calculate string length without spaces in C

1 Answer

0 votes
#include <stdio.h>
#include <string.h>

int calculateLengthWithoutSpaces(const char *str) {
    int length = 0;
    
    while (*str) {
        if (*str != ' ') {
            length++;
        }
        str++;
    }
    
    return length;
}

int main() {
    char str[] = "C is a general-purpose programming language";
    
    int length = calculateLengthWithoutSpaces(str);
    
    printf("%d\n", length);
    
    return 0;
}

   
    
/*
run:
     
38
   
*/

 



answered Jan 11, 2025 by avibootz

Related questions

1 answer 92 views
1 answer 126 views
1 answer 118 views
1 answer 124 views
1 answer 125 views
1 answer 98 views
1 answer 190 views
...