please answer the following question is C In Scrabble each player has a set of t
ID: 3714876 • Letter: P
Question
please answer the following question is C
In Scrabble each player has a set of tiles with letters on them, and the object of the game is to use those letters to spell words. The scoring system is complicated, but as a rough guide longer words are often worth more than shorter words.
Imagine you are given your set of tiles as a String, like "qijibo" and you are given another String to test, like "jib". Write a function called TestWord() that takes these two Strings and returns true if the set of tiles can be used to spell the word. You might have more than one tile with the same letter, but you can only use each tile once.
Explanation / Answer
Hello There,
PFB code for the requested function with appropriate comments and the output of test run:
Code:
------------------------------------------
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
bool TestWord(char *tiles, char *word);
int indexOf(char *tiles, char ch);
int main()
{
char tiles[] = "qijibo";
char word[] = "jib";
printf("%d", TestWord(tiles, word));
return 0;
}
bool TestWord(char *tiles, char *word){
for(int i=0; i<strlen(word); i++){ // check if every char in word is present in tiles
int index = indexOf(tiles, word[i]);
if(index >= 0){
/* if the char is present, update it to a char '#' so that it works correct even if word has multiple copies of same character */
tiles[index] = '#';
}else{
return false;
}
}
return true;
}
int indexOf(char *tiles, char ch){
for(int i=0; i<strlen(tiles); i++){
if(tiles[i] == ch){
return i;
}
}
return -1;
}
------------------------------------
Output of Test Run:
---------------------------------------
1
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.