+ 1
Can anybody help me to solve the given program using C language?
Have the function String Challenge (str) take the string parameter being passed and return the string true if the characters a and b are separated by exactly 3 places anywhere in the string at least once (ie. "lane borrowed" would result in true because there is exactly three characters between a and b). Otherwise return the string false.
16 Answers
+ 4
Tanay
That may work for the string you showed but you don't need the last else if (i == 11) statement. What if you pass a string of a different length?
I would instead do it this way: https://code.sololearn.com/clMTx643197a/#c
+ 3
First of all, you have to give the ab() function the argument s.
Second of all, if you need *three spaces between* you should check the i+4 index, not i+3.
Finally, if you find a match, you should print "true" and terminate the program. But if you find nothing, you should print "false".
+ 3
Gen2oo 👍
Yup if I was the one writing it from a scratch I would have done the same, but after looking at gayathri's version of code I thought it will be hard for her to understand this code so just for sake of simplicity I have just added few comments and modified one or two lines so it can be understood by her. Furthermore, yes I appreciate your version of code, Thanks for sharing it shows how the code should be written.
+ 2
Sure we can. Just please present what you have done so far 👍🏻
+ 2
Gayathri No problem. Would help a lot if you saved the code in the Code Playground and shared a link, instead of just copying it in the question body.
+ 2
Gayathri
Return if "true" is printed within the loop. If the loop exits without returning, then you know the loop found no 3 char separation between a - b so you can print false at the end of the function.
+ 1
Thanks for your help...i will try it out now and check
+ 1
You're also checking past the string buffer. You want the loop condition to end at string length - 4 since you check 4 characters ahead of the string to skip characters between a - b.
I would create a temp variable to hold the string length so it's not calculated N times every loop iteration on compilers that don't cache the result.
And since ab() doesn't modify the string, the function argument could be declared const.
+ 1
Gayathri
I have modified your code with comments let us know if you're still having doubts.
https://code.sololearn.com/coxg0hdz00k9/?ref=app
0
#include <stdio.h>
#include <string.h>
void ab(char[]);
int i;
int main()
{
char s[]="hai blue";
ab();
}
void ab(char s[]){
for(i=0;i<strlen(s);i++)
{
if((s[i]=='a'&&s[i+3]=='b')||(s[i]=='b'&& s[i+3]=='a'))
{
printf ("true");
}}
return 0;
}
0
This is what i have done...
Please help me to find solution for this..
0
Can you plz tell..How do i get false statement to be printed
0
Well,thank.you so much Mr.Tanay...me too got it
0
Can I get full code gayatri
0