0

Stucking somewhere - Change in case

You will be given a single string and two positive integers denoting indices. You need to change the case of the characters at those indices,i.e change uppercase to lowercase and lowercase to uppercase. It is guaranteed that all characters in the string are alphabets. Input: The first line contains N, the length of string. The next line contains a single string. Two integers, x and y, in next line separated by space. Output: Print the string after altering the case of characters at those indices Sample Input: 6 Dcoder 0 3 Sample Output: dcoDer #include <stdio.h> #include <string.h> int main() { int n, a, b, i, j; scanf("%d", &n); char str[n]; scanf("%s", str); scanf("%d %d", &a, &b); for(i=0; i<n; i++) { if(str[i] == str[a] && str[i] < 97) str[a] += 32; else if(str[i] == str[a] && str[i] >= 97) str[a] -= 32; if(str[i] == str[b] && str[i] < 97) str[b] += 32; else if(str[i] == str[b] && str[i] >= 97) str[b] -= 32; printf("%c", str[i]); } return 0; } Getting output dcoder

8th Jun 2021, 6:07 PM
Kashyap Kumar
Kashyap Kumar - avatar
1 Answer
+ 1
Kashyap Kumar Why are you comparing character? If you compare character then after converting 1st character to small, 1st character becoming equal to 3rd index (b) of character, so it is converting 3rd index (b) element to small. That's why 3rd index (b) element is remain same. So you should not compare character. You should compare index value with given inputs. https://code.sololearn.com/ciXKzShLqQ8F/?ref=app
8th Jun 2021, 6:26 PM
A͢J
A͢J - avatar