Exercise 8.3 A word is said to be a “doubloon” if every letter that appears in t
ID: 3711421 • Letter: E
Question
Exercise 8.3 A word is said to be a “doubloon” if every letter that appears in the word appears exactly twice. For example, the following are all the doubloons I found in my dictionary. Abba, Anna, appall, appearer, appeases, arraigning, beriberi, bilabial, boob, Caucasus, coco, Dada, deed, Emmett, Hannah, horseshoer, intestines, Isis, mama, Mimi, murmur, noon, Otto, papa, peep, reappear, redder, sees, Shanghaiings, Toto Write a function called IsDoubloon() that returns TRUE if the given word is a doubloon and FALSE otherwise
Question is in C
Explanation / Answer
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void upper_string(char s[]) {
int c = 0;
while (s[c] != '') {
if (s[c] >= 'a' && s[c] <= 'z') {
s[c] = s[c] - 32;
}
c++;
}
}
int get_occurence_count( char word[50], char letter){
int count = 0;
char capitalWord[50];
strcpy(capitalWord, word);
upper_string(capitalWord);
char capital_letter = letter;
if ( letter >= 'a' &&letter <= 'z') {
capital_letter =letter - 32;
}
int i;
for (i = 0; capitalWord[i] != ''; i++) {
if (capitalWord[i] == capital_letter)
count++;
}
return count;
}
int IsDoubloon(char word[50]){
printf("word is %s", word);
int flag = 1;
int i;
for(i =0; i < strlen(word);i++){
if (get_occurence_count(word, word[i]) != 2){
flag = 0;
break;
}
}
return flag;
}
int main(){
char words[32][50] = {
"Abba",
"Anna",
"appall",
"appearer",
"appeases",
"arraigning",
"beriberi",
"bilabial",
"boob",
"Caucasus",
"coco",
"Dada",
"deed",
"Emmett",
"Hannah",
"horseshoer",
"intestines",
"Isis",
"mama",
"Mimi",
"murmur",
"noon",
"Otto",
"papa",
"peep",
"reappear",
"redder",
"sees",
"Shanghaiings",
"Toto",
"king",
"queen"
};
int i;
for(i = 0; i < 32; i++)
{
printf("string = %s and return Value is %d ", words + i, IsDoubloon(words[i]));
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.