Solved

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

Posted on 2014-11-09
167 Views
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);
}
``````
0
Question by:superflydgreat

LVL 84

Expert Comment

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

LVL 24

Accepted Solution

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]);
``````
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;
}
``````
0

LVL 32

Expert Comment

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

### Suggested Solutions

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…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…