+ 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.

11th Jun 2020, 5:58 PM
Gayathri
Gayathri - avatar
17 Respuestas
+ 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
12th Jun 2020, 7:45 AM
Gen2oo
Gen2oo - avatar
+ 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".
11th Jun 2020, 6:22 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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.
15th Jun 2020, 6:42 PM
Tanay
Tanay - avatar
+ 2
Sure we can. Just please present what you have done so far 👍🏻
11th Jun 2020, 6:07 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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.
11th Jun 2020, 6:53 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 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.
11th Jun 2020, 7:21 PM
Gen2oo
Gen2oo - avatar
+ 1
Thanks for your help...i will try it out now and check
11th Jun 2020, 6:38 PM
Gayathri
Gayathri - avatar
+ 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.
11th Jun 2020, 7:18 PM
Gen2oo
Gen2oo - avatar
+ 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
12th Jun 2020, 6:11 AM
Tanay
Tanay - avatar
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; }
11th Jun 2020, 6:13 PM
Gayathri
Gayathri - avatar
0
This is what i have done... Please help me to find solution for this..
11th Jun 2020, 6:14 PM
Gayathri
Gayathri - avatar
0
Can you plz tell..How do i get false statement to be printed
11th Jun 2020, 6:57 PM
Gayathri
Gayathri - avatar
0
Well,thank.you so much Mr.Tanay...me too got it
12th Jun 2020, 6:57 AM
Gayathri
Gayathri - avatar
0
Can I get full code gayatri
1st Aug 2021, 8:34 AM
Priyatham Maddu
Priyatham Maddu - avatar
0
17th Dec 2021, 5:56 PM
IMRAN KHAN
IMRAN KHAN - avatar