Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <malloc.h>
  3. #include <process.h>
  4. #include <string.h>
  5.  
  6. int get_word(char*s,int *pos,char*w){
  7. int n=strlen(s);
  8. int i=0;
  9.  
  10. while (*pos<n &&s[*pos]<=' ' ){
  11. ++*pos;
  12. }
  13.  
  14. while (*pos<n &&s[*pos]>' ' ){
  15. w[i]=s[*pos];
  16. i++;
  17. ++*pos;
  18. }
  19. w[i]='\0';
  20. return i;
  21. }
  22.  
  23. void SetWord(char s[20], char *a){
  24. int i = 0;
  25. while( s[i] != '\0' ){
  26. a[i] = s[i];
  27. i++;
  28. }
  29. a[i] = '\0';
  30. }
  31.  
  32. void write_arr_str(char *text, char **t, int *n){
  33. int i = 0;
  34.  
  35. char word[20] = {};
  36. while( text[i] != '\0'){
  37. int length = get_word(text,&i,word);
  38. t = (char**)realloc(t, (*n+1)*sizeof(char*));
  39. t[*n] = (char*)calloc(length, sizeof(char));
  40. SetWord(word,t[*n]);
  41. ++*n;
  42. }
  43. }
  44. int main() {
  45. char text1[] = {"vfd fvdvv df vf v dfv f d"};
  46. char text2[] = {" Hello Hello world! world! yes or no? Hello Hello world world You friend? aaa Or no?"};
  47. char **t1 = NULL;
  48. char **t2 = NULL;
  49. int n = 0;
  50. write_arr_str(text1,t1,&n);
  51.  
  52.  
  53. printf("\n");
  54. for(int i = 0; i < n; i++){
  55. puts(t1[i]);
  56. free(t1[i]);
  57. }
  58. free(t1);
  59.  
  60. return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement