Challenge
Given a list of words mixed with extra symbols. Write a program that will clean up the words from extra numbers and symbols.
Specifications
- The first argument is a path to a file.
- Each line includes a test case.
- Each test case is a list of words.
- Letters are both lowercase and uppercase, and mixed with extra symbols.
- Print the words separated by spaces in lowercase letters.
Constraints
- The length of a test case together with extra symbols can be in a range from 10 to 100 symbols.
- The number of test cases is 40.
Input Sample
(--9Hello----World...--)
Can 0$9 ---you~
13What213are;11you-123+138doing7
Output Sample
hello world
can you
what are you doing
Solution:
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
void to_lowercase(char * input) {
for(int i = 0; input[i]; i++){
input[i] = tolower(input[i]);
}
}
char * sanitize(char * input) {
char *sanitized = malloc(sizeof (char) * 1024);
int iterator = 0;
int character_value;
bool wordMatched = false;
for (int i = 0; i < strlen(input); i++) {
int character_value = input[i];
// a-z = 97-122, A-Z = 65-90
if (character_value >= 97 && character_value <= 122
|| character_value >= 65 && character_value <= 90) {
sanitized[iterator++] = input[i];
wordMatched = true;
} else if (wordMatched) {
wordMatched = false;
sanitized[iterator++] = ' ';
}
}
sanitized[iterator] = '\0';
to_lowercase(sanitized);
return sanitized;
}
int main(int argc, const char * argv[]) {
FILE *file = fopen(argv[1], "r");
char line[1024];
while (fgets(line, 1024, file)) {
printf("%s\n", sanitize(line));
}
return 0;
}