In: Computer Science
In the C programming language, implement the translation from regular English to Euroglish. If the letters were uppercase, keep them uppercase in the replacement.
1. Remove one“e”from the end of words that are more than three characters long, if they happen to end in “e”.
2. Change all double letters to a single letter (including double spaces). Do not remove double line spacing (i.e. “\n”).
3. When a word ends in “ed”, change that to just “d”.
Text:
The cat in the hat roamed around the world.
Here is a weird sentence.
We love to code
They fumed and fumed. They laughed and laughed
Better a bird in the hand than two in the tree.
The below program preserves the uppercase letters, removes 'e' from the end of words that are more than three characters long, changes all double letters to a single letter and changes 'ed' with 'd'.
#include <stdio.h>
#include <string.h>
#define MAX_STRINGS 10
#define STRING_LENGTH 50
int main() {
char strings[MAX_STRINGS][STRING_LENGTH];
int i, n, len;
printf("Enter total number of strings: ");
scanf("%d", & n);
printf("Enter the strings:\n");
for (i = 0; i < n; i++) {
printf("String [%d]: ", i + 1);
getchar();
scanf("%[^\n]s", strings[i]);
}
for (i = 0; i < n; i++) {
len = strlen(strings[i]);
// Removes trailing 'e' at the end with no fullstop at the end
if (len > 3 && (strings[i][len - 1] == 'e' || strings[i][len - 1] == 'E')) {
strings[i][len - 1] = '\0';
}
// Runs when the last character of the string is '.'
// Removes trailing 'e' at the end with fullstop at the end
if (len > 4 && strings[i][len - 1] == '.' && (strings[i][len - 2] == 'e' || strings[i][len - 2] == 'E')) {
strings[i][len - 2] = '.';
}
// Removes double characters
for (char * p = strings[i], * q = strings[i];* q++;) {
if ( * p != * q && ++p != q) * p = * q;
}
len = strlen(strings[i]);
// Removes trailing 'ed' with 'e' while preserving capitals
if (len >= 2 && (strings[i][len - 1] == 'd' || strings[i][len - 1] == 'D') && (strings[i][len - 2] == 'e' || strings[i][len - 2] == 'E')) {
if (strings[i][len - 1] == 'd')
strings[i][len - 1] = 'd';
else
strings[i][len - 1] = 'D';
strings[i][len - 1] = '\0';
}
// Runs when the last character of the string is '.'
// Removes trailing 'ed' with 'e' while preserving capitals with fullstop at the end
if (len >= 3 && (strings[i][len - 1] == '.' && strings[i][len - 2] == 'd' || strings[i][len - 2] == 'D') && (strings[i][len - 3] == 'e' || strings[i][len - 3] == 'E')) {
if (strings[i][len - 1] == 'd')
strings[i][len - 1] = 'd';
else
strings[i][len - 1] = 'D';
strings[i][len - 1] = '\0';
}
}
printf("\nStrings are...\n");
for (i = 0; i < n; i++) {
printf("String [%d]: %s\n", i + 1, strings[i]);
}
return 0;
}
Output:
Enter total number of strings: 5
Enter the strings:
String [1]: The cat in the hat roamed around the world.
String [2]: Here is a weird sentence.
String [3]: We love to code
String [4]: They fumed and fumed. They laughed and laughed
String [5]: Better a bird in the hand than two in the tree.
Strings are...
String [1]: The cat in the hat roamed around the world.
String [2]: Here is a weird sentenc.
String [3]: We love to cod
String [4]: They fumed and fumed. They laughed and laughe
String [5]: Beter a bird in the hand than two in the tre.
Note: When code copied from above, student has to replace [NBSP] with space(' ') in the code, if found any.