A string is input which is both in uppercase & lowercase letters. How can I arrange the string so that all lowercase letters are in one side and uppercase letters are in another side.

Dani AI

Generated

A quick clarification: the example in the title ("fgtHyjKLbV" → "fgyjbKLVH") doesn’t match the input (the t is missing and the uppercase order changes), so treat the intended goal as simply: group all lowercase letters to one side and uppercase letters to the other.

As asked about doing it in one array, and as and suggested, the core step is detecting character case. Two practical, commonly used approaches are shown below: an in-place two-pointer partition (O(n) time, O(1) extra space, unstable — does not preserve original relative order) and a stable two-pass copy (O(n) time, O(n) extra space — preserves order). The examples use the standard C character-class functions (see <ctype.h>) and cast to unsigned char before calling them to avoid undefined behaviour on platforms where char is signed.

In-place (two-pointer, swaps in the original array):

#include <ctype.h>
#include <string.h>

void partition_inplace(char *s) {
    size_t i = 0, j = strlen(s);
    if (j == 0) return;
    j--; /* last index */
    while (i < j) {
        while (i < j && islower((unsigned char)s[i])) i++;
        while (i < j && isupper((unsigned char)s[j])) j--;
        if (i < j) {
            char tmp = s[i]; s[i] = s[j]; s[j] = tmp;
            i++; j--;
        }
    }
}

Stable (preserve relative order — uses a new buffer):

#include <ctype.h>
#include <string.h>
#include <stdlib.h>

char *partition_stable(const char *s) {
    size_t n = strlen(s);
    char *out = malloc(n + 1);
    char *p = out;
    for (size_t i = 0; i < n; ++i) if (islower((unsigned char)s[i])) *p++ = s[i];
    for (size_t i = 0; i < n; ++i) if (isupper((unsigned char)s[i])) *p++ = s[i];
    *p = '\0';
    return out; /* caller must free() */
}

Notes and choices: use the in-place version if memory is tight and relative order among letters does not matter; use the stable version if order must be preserved (for input "fgtHyjKLbV" the stable result would be "fgtyjbHKLV"). If the input may contain digits or punctuation, test with isalpha() or adjust the conditions accordingly.

Recommended Answers

All 7 Replies

For basic ASCII which I presume you will be using, then you can just check the character range. and swap accordingly. IE, 65-90 is capital etc

Chris

A string is input which is both in uppercase & lowercase letters. How can I arrange the string so that all lowercase letters are in one side and uppercase letters are in another side.

Upper case characters have the ascii value starting from 65-90 and the lower case characters have the ascii value starting from 97-122.

So i can compare the characters using ascii value and then arrange as you require.

I know we have to swap letters according to ASCII code. But is it possible to arrange it one array? Or the result may come in another array?

You can do it in one array

char array[3] = {'a', 'C', 'b'};
char temp;
temp = array[2];
array[2] = array[1];
array[1] = temp;

Chris

I know we have to swap letters according to ASCII code. But is it possible to arrange it one array? Or the result may come in another array?

yes it is possible to arrange in one array .

yes it is possible to arrange in one array .

Do you feel the need to echo me?

Do you feel the need to echo me?

It is not uncommon for two people to post about the same solution or idea, without knowing or being aware of each other, since the browser doesn't refresh until the post has been committed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.