Sololearn: Learn to Code
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2
If you look at your conditional cases, you can see a lot of duplicate code happening. That is something you can almost always have a look at to see if it can be done better. In terms of the algorithm, I don't think you can really surpass linear complexity (though I might be wrong), but with a bit of pointer fiddling and arithmetic, you can at least get rid of your "reset" variable and one conditional. This is how it could look like: https://code.sololearn.com/cCAo1B6vwwuc/?ref=app However, it is possible to "cheat" the challenge a little by using the way input is provided in the code coach to your advantage. I think this is the shortest you can get in C: https://code.sololearn.com/cCI2bimaOVT0/?ref=app Then again, I might be wrong, given that there exist lots of ways to solve this particular problem.
20th Jan 2021, 1:17 PM
Shadow
Shadow - avatar
+ 1
Good points, Jegix. For context, the code coach uses a simplified set of rules which do not distinguish between vowels and consonants, where you always shift the first letter to end and append "ay". That is the reason both codes pass in this instance. The first one is just a rework of OP's code, but the second one is pretty much a cheat as I already wrote originally, given that it abuses the way input is provided, and meant to be more of a demonstration how short a sufficient code could become. It could still be that argued neither is a hundred percent fine, for example both would fail for an empty input. I guess it depends on how thouroughly you want to be. Your code is certainly a more complete take on the (original) challenge.
21st Jan 2021, 12:51 AM
Shadow
Shadow - avatar
0
Shadow I think neither of those programs meet all the constraints. (But it may be enough to trick your way through the code coach challenge, idk). To my knowledge the rules are: A word beginning with a consonant should have all the consonants preceding the first vowel shifted towards the end and "yay" appended to it. A word beginning with a vowel need only "yay", "way", or "ay" added at the end. This is what I came up with to meet those constraints: https://code.sololearn.com/cA20A24A71a2/#
20th Jan 2021, 8:11 PM
Mike A
Mike A - avatar
0
#include<stdio.h> #include <string.h> #include <stdlib.h> int main() { char input[50]; fgets(input,50, stdin); int length = strlen(input); char temp = input[0]; int p = 0; for(int i=1; i<length;) { if(input[i] ==' ') { printf("%cay ",temp); temp = input[i+1]; i=i+2; } else { printf("%c",input[i]); i++; } } printf("%cay ",temp); return 0; } if have done this after a lot of hardwork
9th Dec 2021, 4:18 AM
Saeendra Varada
Saeendra Varada - avatar