Help please | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Help please

Write your own C reverse_upper function that: takes the supplied input string (in) of a specified length (in_len) AND converts just the lowercase characters to uppercase AND returns the string in reverse order Please do this without using: toupper(), strupr(), strrev(), if, switch or the ? Operator https://code.sololearn.com/crz8oCAg4DZS/?ref=app

4th Apr 2021, 2:29 AM
ONGE
ONGE - avatar
18 Answers
+ 3
ONGE as Hima pointed out, the loop end condition needs to be i>=0. Also remove the & from the &test_str scanf() argument. The array gets translated as a pointer already. To convert to lowercase (LC), first recognize that the difference between UC and LC letters is that their ASCII values differ by 32. In binary that is bit 5. The trick is to force bit 5 to 0 for uppercase. However, if the input string contains characters besides letters, i.e., spaces, numbers or punctuation, then it requires conditionals. In order to accept all input strings without using an 'if' statement or ternary operator, you may still use conditional expressions, which evaluate to either 1 or 0. Multiply that by 32 and then you can conditionally flip the bit. Here are three ways: printf("%c",c^(32*(c>='a' && c<='z'))); printf("%c",c&~(32*(c>='a' && c<='z'))); printf("%c",c - 32*(c>='a' && c<='z'));
4th Apr 2021, 3:25 PM
Brian
Brian - avatar
+ 3
Ipang I was little close ! Thanks to Brian! I should have tried that .
4th Apr 2021, 3:33 PM
Hima
Hima - avatar
+ 2
ONGE That can be implemented using bitwise operators . Moreover, your loop is wrong. It should run till i>=0. #include <string.h> #include<stdio.h> int main() { char str[]="sffaFssF"; for(int i=strlen(str)-1;i>=0;i--) { printf("%c",str[i]^32); } return 0; }
4th Apr 2021, 3:54 AM
Hima
Hima - avatar
+ 1
Hima, Try with a sample string containing digits and spaces. "It all began in December 2019."
4th Apr 2021, 5:22 AM
Ipang
+ 1
Ipang In python3: str=input() dct={'a':'A','b':'B','c':'C','d':'D','e':'E','f':'F','g':'G','h':'H','i':'I','j':'J','k':'K','l':'L','m':'M','n':'N','o':'O','p':'P','q':'Q','r':'R','s':'S','t':'T','u':'U','v':'V','w':'W','x':'X','Y':'y','z':'Z'} for i in str: print(dct.get(i,i),end='') Now, the same can be implemented in C.
4th Apr 2021, 6:45 AM
Hima
Hima - avatar
+ 1
Hima, That is different approach, no bitwise XOR usage in this new snippet. I was referring to the use of bitwise XOR in your original solution, where the alphabets are flip-cased.
4th Apr 2021, 7:22 AM
Ipang
+ 1
Martin Taylor You are absolutely right with your point but doesn't that subdue your creativity :).
4th Apr 2021, 3:33 PM
Hima
Hima - avatar
+ 1
Hima, Yes, Brian gave an excellent solution there! Idk whether or not islower() was allowed, but if it is, it would also help in switching the characters' case.
4th Apr 2021, 3:46 PM
Ipang
+ 1
Ipang Technically , islower does make use of if statement. 😁
4th Apr 2021, 3:49 PM
Hima
Hima - avatar
+ 1
Hima, This is from ctype.h copy in C4Droid. Idk if the definition may have been updated, but in that file it seems to be implemented as macro. #define islower(a) (0 ? islower(a) : ((unsigned)(a)-'a') < 26) But you are right, it uses ternary conditionals there, so I'd say it's out of question 😁
4th Apr 2021, 4:14 PM
Ipang
+ 1
Ipang 😂😂 haha
4th Apr 2021, 4:16 PM
Hima
Hima - avatar
+ 1
Brian without if-else I don't know 😂😂
5th Apr 2021, 12:33 PM
Deepesh Patel
Deepesh Patel - avatar
0
We can get away without `toupper()`, `strupr()`, `strrev()`, `switch`. But without `if`and not even ternary operator, I ask you then, how would you verify whether a char was a lowercase alphabet?
4th Apr 2021, 3:13 AM
Ipang
0
Hey here is a solution, Hope it will help you . #include <stdio.h> #include <string.h> void reverse_str(char a[]) { for (int i=strlen(a)-1;i>=0;i--) { if((97<(int)a[i])&&((int)a[i] <123)) printf("%c",a[i]-32); else printf("%c",a[i]); } } int main(){ char test_str[50]; fgets(test_str ,sizeof(test_str),stdin); reverse_str(test_str); return 0; }
5th Apr 2021, 7:31 AM
Deepesh Patel
Deepesh Patel - avatar
0
@Deepesh patel, Please explain what you did in detail. I beg.
5th Apr 2021, 10:32 AM
Mohale Lepheane
Mohale Lepheane - avatar
0
Deepesh Patel in case you missed it, there are requirements to "... do this without using: toupper(), strupr(), strrev(), if, switch or the ? Operator".
5th Apr 2021, 12:00 PM
Brian
Brian - avatar
0
Mohale Lepheane I just use ASCII values of characters in comparison. Every character has an ASCII value of int type.you may search google for it. For A-Z it is 65 to 90 For a-z it is 97 to 122
5th Apr 2021, 12:38 PM
Deepesh Patel
Deepesh Patel - avatar
0
Deepesh Patel to date, the proposed solutions are to use a lookup table or use Boolean expressions. See the examples in prior replies to learn more.
5th Apr 2021, 12:59 PM
Brian
Brian - avatar