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

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

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

1 Answer

0 votes
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
  
void move_uppercase_to_end(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] = lowercase[i];
    }
    for (int i = lowercase_i, j = 0; i < len; i++, j++) { 
        s[i] = uppercase[j];
    }
          
    free(lowercase);
    free(uppercase);
} 
     
int main() 
{ 
    char arr[] = "C++ PHP Java"; 
      
    move_uppercase_to_end(arr, strlen(arr));
      
    puts(arr);
       
    return 0; 
} 
     
     
     
/*
run:
     
++  avaCPHPJ
   
*/

 



answered Aug 18, 2019 by avibootz
edited Aug 20, 2019 by avibootz

Related questions

1 answer 159 views
1 answer 152 views
1 answer 166 views
1 answer 161 views
1 answer 146 views
1 answer 383 views
...