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