Avatar of superflydgreat
superflydgreat
 asked on

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 10

int hydroxide(char *compound);

int
main(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);
}

Open in new window

Algorithms

Avatar of undefined
Last Comment
phoffric

8/22/2022 - Mon
ozo

what was
  strcpy(end[], &compound[last - 2]);
intended to do?
ASKER CERTIFIED SOLUTION
chaau

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
phoffric

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".
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23