How can I use a c function to determine what a string ends in?
The program is supposed to use the hydroxide function to check an inputted string to see if it ends in oh (or ho) and return a 1 value if it does. Here is what I have written and when I run the program it just freezes. I am still a beginner so I apologize in advance if this is a stupid question.
#include <stdio.h>#include <string.h>#define MAX_LEN 10int hydroxide(char *compound);intmain(void){ char compound[MAX_LEN]; int i, num; printf("Enter compound> \n"); scanf("%s", compound); for (i = 0; i < strlen(compound); ++i) { if (islower(compound[i])) compound[i] = toupper(compound[i]); } num = hydroxide(compound); printf("%d", num); return(0);}int hydroxide(char *compound){ char *end[4], *temp; int last, status; last = strlen(compound); strcpy(end[], &compound[last - 2]); if (strcmp(end[last - 2],end[last - 1]) > 0) { temp = end[last - 2]; end[last - 2] = end[last - 1]; end[last - 1] = temp; } if (*end[last - 2] == 'H') { if (*end[last - 1] == 'O') status = 1; } return(status);}
Since this is a learning exercise (either self-study or school assignment), we cannot give you complete solutions per our terms of service.
Here is a suggestion. Write a function called isOHorHOatEndOfString, which takes in the string and uses the following library function, strrchr - "Locate last occurrence of character in string" http://www.cplusplus.com/reference/cstring/strrchr/
Notice that the return values is "A pointer to the last occurrence of character in str".
strcpy(end[], &compound[last - 2]);
intended to do?