Toggle Case – Reversal

The program must accept a string S containing only alphabets and an integer X as the input. The program must toggle the case of the first X alphabets in S and print them in reverse order as the output.

Boundary Condition(s):
1 <= Length of S <= 100
1 <= X <= Length of S

Example Input/Output 1:
Input:
ABCDefghaB
5
Output:
Edcba

Example Input/Output 2:
Input:
ANgeLGODdess
7
Output:
oglEGna

s = input().strip()
x = int(input())

for i in range(x-1, -1, -1):
    if 'a' <= s[i] <= 'z':
        print(s[i].upper(), end="")
    else:
        print(s[i].lower(), end="")
#include <stdio.h>
#include <ctype.h>

int main() {
    char s[101];
    int x;
    scanf("%s", s);
    scanf("%d", &x);

    for (int i = x-1; i >= 0; i--) {
        if (islower(s[i])) {
            putchar(toupper(s[i]));
        } else {
            putchar(tolower(s[i]));
        }
    }

    return 0;
}
#include <iostream>
#include <cctype>
using namespace std;

int main() {
    string s;
    int x;
    cin >> s >> x;

    for (int i = x-1; i >= 0; i--) {
        if (islower(s[i])) {
            cout << (char)toupper(s[i]);
        } else {
            cout << (char)tolower(s[i]);
        }
    }

    return 0;
}
import java.util.Scanner;

public class ToggleCaseReversal {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[] chars = scanner.next().toCharArray();
        int x = scanner.nextInt();

        for (int i = x-1; i >= 0; i--) {
            if (Character.isLowerCase(chars[i])) {
                System.out.print(Character.toUpperCase(chars[i]));
            } else {
                System.out.print(Character.toLowerCase(chars[i]));
            }
        }
    }
}
Previous Article

Twisted Prime

Next Article

Consonant - Vowel Pairs

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *