Solved

How can I use a c function to determine what a string ends in?

Posted on 2014-11-09
3
178 Views
Last Modified: 2014-11-09
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

0
Comment
Question by:superflydgreat
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 40432019
what was
  strcpy(end[], &compound[last - 2]);
intended to do?
0
 
LVL 25

Accepted Solution

by:
chaau earned 500 total points
ID: 40432029
In your hydroxide function there are a couple of problems.
First of all, temp must be declared as char, not char*
When using strcpy, use the actual variable, without []:
strcpy(end, &compound[last - 2]);

Open in new window

And lastly, in the very last statement you need to check the actual characters at position [last - 2] and [last - 1], not their addresses, i.e. drop *:
if (end[last - 2] == 'H') {
		if (end[last - 1] == 'O')
			status = 1;
	}

Open in new window

0
 
LVL 32

Expert Comment

by:phoffric
ID: 40432051
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".
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question