0
Anyone knows how to do this question? I'm confused..
Write and test a function hydroxide that returns a 1 for true if its string argument ends in the substring OH . Try the function hydroxide on the following data:KOH H2O2 NaCl NaOH C9H8O4 MgOH
18 Antworten
+ 3
char compound[5];
scanf("%s\n",compound);
if(strstr(compound,"OH")!=NULL)
//If there was OH inside the name...
{
printf("Yes, %s is a Hydroxide\n",
compound);
return 1;
}
else
//Otherwise...
{
printf("No, %s is not a hydroxide",
compound);
return 0;
}
Here. Let me know if this worked...
+ 2
@A7med
The strstr function automatically iterates over the entire string. Then why do you still need to use a for loop? And secondly : strstr(str[i],"OH") ;
Is a horrible error. Firstly you compare a character with a string (Invalid) and secondly, you pass a character to a function that works only on strings.
And now I guess you may tailor the code to your own needs yourself, now that you have got the way. As I am still not able to understand what you want, and why you still want a for loop when I provided you a solution using strstr which is so simpler...
If you use for, you need to check all characters of the string two at a time and then you may compare them with strstr, after you add them into a string. But as you can see, this is tedious to do and it is best to use a built in function.
The for will look like this:
for(int i=0; i<strlen(molecule)-1; i++)
{
char arr[3];
arr[0] = molecule[i];
arr[1] = molecule[i+1];
arr[2] = '\0';
if(strstr(arr,"OH")!=NULL) return 1;
else continue;
}
return 0;
// Outside for, otherwise won't read the
// entire string.
+ 1
@kinshuk Vasishy
than I you for the code you have made but I mean C program, like using scanf printf and all that
+ 1
@kinshuk vasisht
I'm sorry mate... I just started using this app...🔫 CA(OH) and the others are basically the inputs to test it whether it's right or wrong. get it ?
+ 1
@kinshuk Vasish
like you would need to use printf function to get the data which are these elements Like CA(OH)2 to see if it's true or false
+ 1
@kinshuk variant
thank you a lot for the code mate but like thing is, we need to ask the user to enter suppose a molecule or so, Then put it into a variable then check if last 2 words of that molecule has OH or no, where if so then it will show true otherwise
+ 1
yes, basically getting something from the user like say NaCI or NaOH then to check if the last 2 characters are OH. where if there is then it's true or else if not then false. sorry to confuse you. I appreciate your help😄
0
int hydroxide (string molecule)
{
extract the last 2 characters.
look if they equal "OH".
if yes: return 1.
else return 0 or -1 or anything except 1 (not specified).
}
0
yes I understand the question timon but I want to know how to code it 🔫
0
@A7med
Your function should also work for Ca(OH)2 or Al(OH)3, right?
#include<regex>
int Hydroxide(string compound)
// Returns the number of matching
// hydroxides, and prints them...
{
int ctr=0;
regex pattern ("(\\b\\w{1,3}[(]?OH+[)]?\\d*\\b)");
smatch res;
while (regex_search(compound, res, pattern))
{ cout << res[0] << endl;
compound = res.suffix();
ctr++; }
return ctr;
}
0
@A7med
You tagged C++, so I gave a c++ code.
That will be quite longer than this, and may not even work for all inputs...
Give me some time to make the code.
Now, will you have Ca(OH)2 in input or not?
0
@A7med
Here. Let me know if you need anything more.
https://code.sololearn.com/ckOqV7ljc36q/?ref=app
0
@kinshuk Vasisht, like there must be printf function to get it then to save into variable then to check if true or false, and the code you sent idk something wrong with it, like I dont get it? help please? I appreciate it
0
@A7med
You want to read for eg - NaOH and check if last 2 chars are OH or not right?
I thought you read all the data at once and wanted to read one by one from the data string. My function does that.
And you can't just match the last two characters as then the criteria fails for Ca(OH)2, etc.
Ill make a simpler function, but will keep that one in the program too. (Perhaps you may need it).
And if you need the input like this:
Enter Molecule - NaOH
That is, with the prompt side by side, you need to use a PC, as Sololearn doesn't support reading inputs at runtime.
I created a function and the line - strstr(ptr,"OH") searches for OH in the compound formula Ill rewrite...
0
@kinshuk Vasish
Umm I guess it's close. basically after getting what the user wants, then put it into variable compound. Then checking each element in the string to see if there is OH included or no. where if so then return 1 or else return 0
I wrote it Like this
#include <stdio. h>
#include <string.h>
int main (void)
{
char compound[5];
char comp;
char i;
printf("Enter a compound:");
scanf("%s", comp
for(i>0 ; i<strlen(comp);++i)
{
if strstr(comp[i],"OH")!=NULL)
{
return (0);
else
return(1);
0
like I want to read whatever the user typed in pc, then read it and see if OH "ends" in substring OH ( read the question) then use if statement to see if it's true or no
0
@timom
firstly it isn't a homework, secondly you can just simply ignore it if you aren't willing to help.. it's okay.. No need to be dramatic bro. thank you!
- 1
You're pretty outrageous, A7med. You don't know how to check for input and have a vague question and you say to us things like "Read the question properly.". This seems to be your homework anyways, so do it without at least my help.