// Find words in the list that start with q but not qu // DSM, 2016 #include <st
ID: 3924062 • Letter: #
Question
// Find words in the list that start with q but not qu // DSM, 2016
#include <stdio.h> #include <stdlib.h> // For perror and exit #include <string.h>
//*** Tests a word ***// // // It's helpful to put complex tests in their own functions, rather than // in the basic file-reading loop. int startsWithQButNotQu(char *word) { if (word[0] == 'q' && word[1] != 'u') { return 1; } else { return 0; } }
//*** Reads and tests every word in the list ***// void readWordList() { FILE *f = fopen("words.txt", "r"); if (f == NULL) { perror("fopen"); exit(1); } char buf[64]; while (fgets(buf, sizeof(buf), f) != NULL) { buf[strlen(buf) - 1] = ''; // Strip terminating newline if (startsWithQButNotQu(buf)) { printf("%s ", buf); } } fclose(f); return; }
int main() { readWordList(); return 0; }
Explanation / Answer
1)
program1.c
#include <stdio.h>
#include <stdlib.h> // For perror and exit
#include <string.h>
//*** Tests a word ***//
//
// It's helpful to put complex tests in their own functions, rather than
// in the basic file-reading loop.
int checkVowelsAndY(char *word) {
int i;
for(i=0;i<word.length();i++){
if(word[i] == 'a' || word[i] == 'e' || word[i] == 'i' || word[i] == 'o' || word[i] == 'u' || word[i] == 'y'){
return 0;
}
}
return 1;
}
//*** Reads and tests every word in the list ***//
void readWordList() {
FILE *f = fopen("words.txt", "r");
if (f == NULL) {
perror("fopen");
exit(1);
}
char buf[64];
while (fgets(buf, sizeof(buf), f) != NULL) {
buf[strlen(buf) - 1] = ''; // Strip terminating newline
if (checkVowelsAndY(buf)) {
printf("%s ", buf);
}
}
fclose(f);
return;
}
int main() {
readWordList();
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
2)
problem2.c
#include <stdio.h>
#include <stdlib.h> // For perror and exit
#include <string.h>
//*** Tests a word ***//
//
// It's helpful to put complex tests in their own functions, rather than
// in the basic file-reading loop.
int checkDoublePairs(char *word) {
int i,count=0;
if(word.length() >= 6){
for(i=0;i<word.length()-1;i++){
if(word[i] == word[i+1]){
count++;
}
}
if(count >= 3){
return 1;
}
}
return 0;
}
//*** Reads and tests every word in the list ***//
void readWordList() {
FILE *f = fopen("words.txt", "r");
if (f == NULL) {
perror("fopen");
exit(1);
}
char buf[64];
while (fgets(buf, sizeof(buf), f) != NULL) {
buf[strlen(buf) - 1] = ''; // Strip terminating newline
if (checkDoublePairs(buf)) {
printf("%s ", buf);
}
}
fclose(f);
return;
}
int main() {
readWordList();
return 0;
}
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
3)
problem3.c
#include <stdio.h>
#include <stdlib.h> // For perror and exit
#include <string.h>
//*** Tests a word ***//
//
// It's helpful to put complex tests in their own functions, rather than
// in the basic file-reading loop.
int checkPalindrome(char *word) {
if(strrev(word) == word){
return 1;
}
return 0;
}
//*** Reads and tests every word in the list ***//
void readWordList() {
FILE *f = fopen("words.txt", "r");
if (f == NULL) {
perror("fopen");
exit(1);
}
char buf[64];
while (fgets(buf, sizeof(buf), f) != NULL) {
buf[strlen(buf) - 1] = ''; // Strip terminating newline
if (checkPalindrome(buf)) {
printf("%s ", buf);
}
}
fclose(f);
return;
}
int main() {
readWordList();
return 0;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------
(4)
problem4.c
#include <stdio.h>
#include <stdlib.h> // For perror and exit
#include <string.h>
//*** Tests a word ***//
//
// It's helpful to put complex tests in their own functions, rather than
// in the basic file-reading loop.
int checkUniqueLetters(char *word) {
int chars[word.length()];
int i,res = 0,index = 0;
for(i=0;i<word.length();i++){
res = 0;
for(j=0;j<index;j++){
if(word[i] == chars[j]){
res = 1;
}
}
if(res == 0){
chars[index] = word[i];
index++;
}
}
return index;
}
//*** Reads and tests every word in the list ***//
void readWordList() {
FILE *f = fopen("words.txt", "r");
if (f == NULL) {
perror("fopen");
exit(1);
}
char buf[64];
char maxword[64];
int max = 0;
while (fgets(buf, sizeof(buf), f) != NULL) {
buf[strlen(buf) - 1] = ''; // Strip terminating newline
if(checkUniqueLetters(buf) > max){
max = checkUniqueLetters(buf);
maxword = buf;
}
}
printf("%s",maxword);
fclose(f);
return;
}
int main() {
readWordList();
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.