How to count the number of characters in a string without spaces and special characters in C

1 Answer

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

void remove_non_lphabetic_characters(char str[]) {
    int length = strlen(str);
    int j = 0;
     
    for (int i = 0; i < length; i++) {
        if (isalpha(str[i])) {
            str[j++] = str[i];
        }
    }
    str[j] = '\0'; // Null-terminate the modified string
}
    
int main() {
    char str[] = "8go@ c# rust java 123 c &c++.";
    
    remove_non_lphabetic_characters(str);
    
    int result = strlen(str);

    printf("%d\n", result);

    return 0;
}


/*
run:
    
13
    
*/

 



answered Sep 15, 2024 by avibootz
...