fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <locale.h>
  6. #include <stdint.h>
  7.  
  8.  
  9. // separate words with only one space
  10. char *handle_whitespace(char *content)
  11. {
  12. // to be removed later
  13. setlocale(LC_ALL, "en_US.utf8");
  14. int i, len = mbstowcs(NULL,content,0)+1;
  15.  
  16. wchar_t unicode_content[len];
  17. wchar_t *normalized_content = malloc(len * sizeof(wchar_t *));
  18.  
  19. // convert char to wchar_t
  20. mbstowcs(unicode_content, content, len);
  21.  
  22. short space_added = 0;
  23. int k=0;
  24. for(i=0; unicode_content[i] ; ++i){
  25. if(iswspace(unicode_content[i])){
  26. if(!space_added) {
  27. normalized_content[k] = L' ';
  28. space_added = 1;
  29. k++;
  30. }
  31. }else {
  32. normalized_content[k] = unicode_content[i];
  33. space_added = 0;
  34. k++;
  35. }
  36.  
  37. }
  38. normalized_content[k+1] = L'\0';
  39.  
  40. // convert wchar_t back to char
  41. char *newstr = malloc(len * sizeof(char *));
  42. wcstombs(newstr,normalized_content,len * sizeof(char *));
  43.  
  44. free(normalized_content);
  45.  
  46. return newstr;
  47. }
  48.  
  49. int main(){
  50. char *h = handle_whitespace("hello world");
  51.  
  52. printf("%s\n", h);
  53. return 0;
  54. }
Success #stdin #stdout 0s 2184KB
stdin
Standard input is empty
stdout
hello world