Solved

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

Posted on 2014-11-09
3
174 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
3 Comments
 
LVL 84

Expert Comment

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

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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Okay. So what exactly is the problem here? How often have we come across situations where we need to know if two strings are 'similar' but not necessarily the same? I have, plenty of times. Until recently, I thought any functionality like that wo…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
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…

808 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