Thread: Sorting string alphabetically

  1. #1
    Registered User
    Join Date
    Jun 2018
    Posts
    1

    Sorting string alphabetically

    Hi!!
    I need to sort a sting if capital and/or small letters in the orders of letters only, so that a caputal letter is first
    example:
    input: PrintCapitalLettersBeforeSmallLetters
    output: aaaBCeeeeeefiiLLlllmnoPprrrrSsstttttt

    I wrote a code but i only manage to order capital letters first:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main (void) {
    char string[] = "SimplYeAsyLearning";
    char temp;
    
    int i, j;
    int n = strlen(string);
    
    printf("String before sorting - %s \n", string);
    
    for (i = 0; i < n-1; i++) {
    for (j = i+1; j < n; j++) {
    if (string[i] > string[j]) {
    
    temp = string[i];
    string[i] = string[j];
    string[j] = temp;
    }
    }
    }
       
    printf("String after sorting - %s \n", string);
    return 0;
    }
    I need to make another comparison but I dontknow how.
    I can only use #include <stdio.h> inside the code

    Thank you!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I can only use #include <stdio.h> inside the code
    But you've already included string.h and called strlen.

    Perhaps the answer is to write your own version of tolower()
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting alphabetically in C using strcmp
    By BlackDeath in forum C Programming
    Replies: 7
    Last Post: 04-16-2015, 10:03 AM
  2. Sorting string within struct alphabetically
    By omGeeK in forum C Programming
    Replies: 8
    Last Post: 02-17-2011, 11:48 PM
  3. Alphabetically sorting argv[]
    By serg_yegi in forum C Programming
    Replies: 7
    Last Post: 03-27-2010, 07:25 PM
  4. sorting alphabetically
    By ahming in forum C Programming
    Replies: 8
    Last Post: 05-26-2005, 10:34 AM
  5. Sorting Alphabetically
    By orgy230 in forum C Programming
    Replies: 1
    Last Post: 12-04-2004, 02:28 PM

Tags for this Thread