codeblocks, c code, using only stdio.h and string.h Question 1: Write a function
ID: 3679995 • Letter: C
Question
codeblocks, c code, using only stdio.h and string.h
Question 1:
Write a function hydroxide that returns 1 for true if its string argument ends in the substring OH.
Question 2:
Write a program that takes nouns and forms their plurals based on these rules:
1. If noun ends in "y" remove the "y" and add "ies".
2. if noun ends in "s" , "ch", or "sh", add "es"
3. in all other cases, just add "s".
Question 3:
Write a function that finds and returns through an output parameter the longest common suffix of two words.
Explanation / Answer
Question 1:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define FALSE 0
#define TRUE 1
/*Function: isHydroxide
Purpose: determine if input string ends in oh or OH
Arguments: none
Returns: TRUE or FALSE */
int isHydroxide(char* str, int size);
int isHydroxide(char* str, int size)
{
//checks that it ends in OH or oh
if(str[size] == 0x4F || str[size] == 0x6F)
if(str[size - 2] == 0x48 || str[size - 1] == 0x68)
return TRUE; //if it contains OH
return FALSE; //if it does not contain OH
}
/*Function: main
Purpose: determine if compound ends in oh or OH
Arguments: none
Returns: string, 0 upon completion*/
int main (int argc, char **argv)
{
char *input = (char *)malloc(512); //allocate memory for string
int i = 0;
printf(" Enter a chemical compound: ");
scanf("%s", input); //get string input
//Finds the length of the array minus 1
while(input[i] != '')
i ++;
if(isHydroxide(input, i))
printf(" The compound %s is a hydroxide ", input);
else
printf(" The compound %s is not a hydroxide ", input);
return 0;
}
Question 2:
#include <stdio.h>
#include <string.h>
#define MAX_WORD 50
/* prototypes */
void pluralize (char word[]);
int main (void) {
/* declarations */
char temp_word[MAX_WORD]; /* stores temporary word entered by user */
/* prompt user to enter noun */
printf("This program prints the plural form of a noun. ");
printf("Please type in a noun in singular form: ");
scanf("%s", temp_word);
while (strcmp(temp_word, "done") != 0) {
/* convert noun into plural form */
pluralize (temp_word);
/* print the plural form of the word */
printf("The plural form is %s ", temp_word);
/* prompt user to enter noun */
printf("Please type in a noun in singular form: ");
scanf("%s", temp_word);
}
/* return successfully */
return 0;
}
/* pluralize
* parameters: char word[]
* return values: none, but word[] will be changed through this function
* functionality: changes word[] to be the plural form of word[]
*/
void pluralize (char word[]){
/* declarations */
int length;
/* find length of word */
length = strlen(word);
/* check first rule: if word ends in "y" then change to "ies" */
if (word[length - 1] == 'y') {
word[length - 1] = 'i';
word[length] = 'e';
word[length + 1] = 's';
word[length + 2] = ''; /* remember to put '' at end of string */
}
/* check second rule: if word ends in "s" "ch" or "sh" add "es" */
else if (word[length - 1] == 's' ||
(word[length - 2] == 'c' && word[length - 1] == 'h') ||
(word[length - 2] == 's' && word[length - 1] == 'h')){
/* concatenate "es" to word */
strcat(word, "es");
}
/* otherwise, just add "s" to the end of word */
else {
strcat(word, "s");
}
}
Question 3:
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.