Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Bonsai Boy of New York Bonsai Trees and Accessories

BlueHost Web Hosting

Liquid Web iThemes WordPress Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

DreamHost Web Hosting


Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Liquid Web Cloud VPS Hosting

10,939 questions

14,849 answers

573 users

How to move all special characters to the end of char array in C

1 Answer

0 votes
#include <stdio.h> 
#include <string.h> 
#include <stdlib.h> 
#include <ctype.h> 

void move_special_characters_to_end(char *s, int len) { 
    char *chars = (char *)malloc(len + 1 * sizeof(char));
	char *special_characters = (char *)malloc(len + 1 * sizeof(char));
	int chars_i = 0, special_characters_i = 0;
	
    for (int i = 0; i < len; i++) { 
        char ch = s[i]; 
        if (isalnum(ch) || ch == ' ') 
            chars[chars_i++] = ch; 
        else
            special_characters[special_characters_i++] = ch; 
    } 
	for (int i = 0; i < chars_i; i++) { 
		s[i] = chars[i];
	}
	for (int i = chars_i, j = 0; i < len; i++, j++) { 
		s[i] = special_characters[j];
	}
		
	free(chars);
	free(special_characters);
} 
   
int main() 
{ 
    char arr[] = "c++$vb.net&%java*() php <>/python 3.7.3"; 
	
    move_special_characters_to_end(arr, strlen(arr));
	
	puts(arr);
     
    return 0; 
} 
   
   
   
/*
run:
   
cvbnetjava php python 373++$.&%*()<>/..
 
*/

 




answered Aug 12 by avibootz
edited Aug 13 by avibootz
...