?
Solved

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

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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

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…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

719 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