fork(1) download
  1. #include <stdio.h>
  2.  
  3. char *handle_whitespace(char *content);
  4.  
  5. int main(void) {
  6. // your code goes here
  7. return 0;
  8. }
  9.  
  10. // function to keep only one white space between words utf8
  11. char *handle_whitespace(char *content)
  12. {
  13. // to be removed later
  14. setlocale(LC_ALL, "en_US.utf8");
  15. // calc length of *content
  16. int i, len = mbstowcs(NULL,content,0)+1;
  17.  
  18. // will hold *content as utf8
  19. wchar_t unicode_content[len];
  20. // contain the new string
  21. wchar_t *normalized_content = malloc(len * sizeof(wchar_t *));
  22.  
  23. // convert char to wchar_t
  24. mbstowcs(unicode_content, content, len);
  25.  
  26. short space_added = 0;
  27. for(i=0; unicode_content[i] ; ++i){
  28. if(iswspace(unicode_content[i])){
  29. if(!space_added) {
  30. normalized_content[i] = L' ';
  31. space_added = 1;
  32. }
  33. } else {
  34. normalized_content[i] = unicode_content[i];
  35. space_added = 0;
  36. }
  37. }
  38. normalized_content[i+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. return newstr;
  46. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'handle_whitespace':
prog.c:14:4: warning: implicit declaration of function 'setlocale' [-Wimplicit-function-declaration]
    setlocale(LC_ALL, "en_US.utf8");
    ^
prog.c:14:14: error: 'LC_ALL' undeclared (first use in this function)
    setlocale(LC_ALL, "en_US.utf8");
              ^
prog.c:14:14: note: each undeclared identifier is reported only once for each function it appears in
prog.c:16:4: warning: implicit declaration of function 'mbstowcs' [-Wimplicit-function-declaration]
    int i, len = mbstowcs(NULL,content,0)+1;
    ^
prog.c:19:4: error: unknown type name 'wchar_t'
    wchar_t unicode_content[len];
    ^
prog.c:21:4: error: unknown type name 'wchar_t'
    wchar_t *normalized_content = malloc(len * sizeof(wchar_t *));
    ^
prog.c:21:4: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
prog.c:21:34: warning: incompatible implicit declaration of built-in function 'malloc'
    wchar_t *normalized_content = malloc(len * sizeof(wchar_t *));
                                  ^
prog.c:21:54: error: 'wchar_t' undeclared (first use in this function)
    wchar_t *normalized_content = malloc(len * sizeof(wchar_t *));
                                                      ^
prog.c:21:63: error: expected expression before ')' token
    wchar_t *normalized_content = malloc(len * sizeof(wchar_t *));
                                                               ^
prog.c:28:6: warning: implicit declaration of function 'iswspace' [-Wimplicit-function-declaration]
      if(iswspace(unicode_content[i])){
      ^
prog.c:42:4: warning: implicit declaration of function 'wcstombs' [-Wimplicit-function-declaration]
    wcstombs(newstr,normalized_content,len * sizeof(char *));
    ^
prog.c:44:4: warning: implicit declaration of function 'free' [-Wimplicit-function-declaration]
    free(normalized_content);
    ^
prog.c:44:4: warning: incompatible implicit declaration of built-in function 'free'
stdout
Standard output is empty