I want to sort single string in alphabetical order what wrong in my code please help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

I want to sort single string in alphabetical order what wrong in my code please help

Statement:create a program such that it accepts a string S of N characters. S should only contains upper and lower-case characters. Numbers and special case characters are not allowed in S.Now rearrange the string S in alphabetical order (a to z) . In order to modify the string S consider the following rule. Do not differentiate between lower-case and upper-case character of same alphabet and if both cases are available of same character preserve the order as of the original string. For example, if string S is “CxBdbazA” here alphabet “b” and “a” both present in upper case and lower case. but upper case “B” appears before lower case “b” and lower-case “a” appears before upper-case “A” so in modified string alphabet orders will be “aABbCdxz”. Input:first line contains string S of contains N characters without space. Output: output contains the rearranged string S. Constraints:5<=N<=25 take character array size as 25 to store string. Example: Hello eHllo https://code.sololearn.com/c5YsbHJ5JnDo/?r

20th Apr 2021, 11:28 AM
rahul barhate
rahul barhate - avatar
3 Answers
+ 3
your for 2nd for loop condition wasn't right and your upper function was a bit wrong i guess so i used an in built function and it works now here the code: #include <stdio.h> #include<string.h> #include<ctype.h> int main() { char s[20]; char t; int r,i,len; scanf("%s",s); len=strlen(s); for(r=0;r<len-1;r++) { for(i=0;i<len-1-r;i++) { if(tolower(s[i])>tolower(s[i+1])) { t=s[i]; s[i]=s[i+1]; s[i+1]=t; } } } printf("%s",s); return 0; } https://code.sololearn.com/cL4Q46IXfkRC/?ref=app
20th Apr 2021, 12:42 PM
Seamus
Seamus - avatar
+ 3
Thanks both of you 😊😊
20th Apr 2021, 1:38 PM
rahul barhate
rahul barhate - avatar
+ 2
You didn't change the value of b[]. As you are comparing with with b string, if(b[r]>b[i]) you also have to change the value of it.I am not sure but it should work: https://code.sololearn.com/cA20a18a22a9
20th Apr 2021, 12:43 PM
The future is now thanks to science
The future is now thanks to science - avatar