Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,070 questions

40,776 answers

573 users

How to move all uppercase characters to the beginning of string in C

1 Answer

0 votes
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
 
void move_uppercase_to_beginning(char *s, int len) { 
    char *lowercase = (char *)malloc(len + 1 * sizeof(char));
    char *uppercase = (char *)malloc(len + 1 * sizeof(char));
    int uppercase_i = 0, lowercase_i = 0;
	char ch;
	
    for (int i = 0; i < len; i++) { 
        ch = s[i]; 
        if (ch >= 'A' && ch <= 'Z') 
            uppercase[uppercase_i++] = ch; 
        else
           lowercase[lowercase_i++] = ch; 
    } 
    for (int i = 0; i < lowercase_i; i++) { 
        s[i] = uppercase[i];
    }
    for (int i = uppercase_i, j = 0; i < len; i++, j++) { 
        s[i] = lowercase[j];
    }
         
    free(lowercase);
    free(uppercase);
} 
    
int main() 
{ 
    char arr[] = "C++ PHP Java"; 
     
    move_uppercase_to_beginning(arr, strlen(arr));
     
    puts(arr);
      
    return 0; 
} 
    
    
    
/*
run:
    
CPHPJ++  ava
  
*/

 





answered Aug 20, 2019 by avibootz
...