Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-11-09
3
Medium Priority
?
209 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 85

Expert Comment

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

Accepted Solution

by:
chaau earned 1500 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
When there is a disconnect between the intentions of their creator and the recipient, when algorithms go awry, they can have disastrous consequences.
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

926 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