Removing all characters from string which are not letters and changing every first letter of a word to capital letter

Viewed 57

I need help with my program in C. My task is too remove all characters which are not letters and change every first letter of a word to capital letter and return the number of deleted non-letters, for example "i love programming!" to "ILoveProgramming", it should prop return 3 (number of deleted non letters) . I cannot use the <ctype.h>, <stdlib.h> or <string.h>headers,[]only to declare array, and I can only use numbers1and0. Also I can't change the function definition or do anything with that (int message_compression(char * txt);`). So far I managed to change letters but I have no idea how to remove characters which are not letters.

#include <stdio.h>
#include <math.h>

int message_compression(char* txt);

int main() {
    char txt[1001];
    char *wt;
    wt = txt;
    puts("Text:");
    fgets(txt, 1001, stdin);
    message_compression(wt);
    int i = 0;
    printf("After change:\n");
    while (*wt != '\0') {
        printf("%c", *wt);
        i++;
        *wt = *(wt + i);
    }
    return 0;
}

int message_compression(char* txt) {
    if (txt == NULL) {
        return -1;
    }
    int i = 0;
    while (*(txt + i) != '\x0') {
        if (i == 0) {
            *(txt + i) = *(txt + i) - ' ';
        }
        if (*(txt + i) == ' ' || *(txt + i) == ',' || *(txt + i) == '.') {
            if (*(txt + i + 1) != ' ' && *(txt + i + 1) != ',' && *(txt + i + 1) != '.') {
                *(txt + i + 1) = *(txt + i + 1) - ' ';
            }
        }
        i++;
    }
    return 0;
}
1 Answers

If you are allowed to define other functions such as is_letter() and to_capital() and use them in your implementation of message_compression(), here is pseudo code for a simple solution:

  • let i = 0 the read index, and j = 0 the write index
  • repeat:
    • repeat while character at offset i is not a letter:
      • if the character at offset i is the null terminator set a null terminator at offset j and return i - j
      • otherwise increment i
    • store the character to_capital(character at offset i) at offset j
    • increment i and j
    • repeat while character at offset i is a letter:
      • copy the character at offset i to offset j
      • increment i and j

Try and code it in C within the constraints imposed for the assignment.

Here is a simplified framework:

#include <stdio.h>

int is_letter(char c) {
    // simple solution for ASCII
    return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}

char to_capital(char c) {
    // simple solution for ASCII
    if (c >= 'a' && c <= 'z')
        return 'A' + (c - 'a');
    else
        return c;
}

int message_compression(char* txt) {
    int i = 0, j = 0;
    for (;;) {
        while (!is_letter(txt[i])) {
            if (txt[i] == '\0') {
                txt[j] = '\0';
                return i - j;
            } else {
                i++;
            }
        }
        txt[j++] = to_capital(txt[i++]);
        while (is_letter(txt[i])) {
            txt[j++] = txt[i++];
        }
    }
}

int main() {
    char txt[1001];

    puts("Text:");
    if (fgets(txt, sizeof txt, stdin)) {
        int deleted = message_compression(txt);
        printf("After change: %s\n", txt);
        printf("Characters removed: %d\n", deleted);
    }
    return 0;
}
Related