Advertisement
Guest User

Untitled

a guest
Dec 17th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int countWords(char* text,char separator);
  7. void calculateLengthOfWords(char* text,char separator, int* wordsLength);
  8. int main()
  9. {
  10. char text[]="Jebac jebac i sie nie dac";
  11. char separator=' ';
  12. int numberOfWords=countWords(text,separator);
  13. int* wordsLength=(int*)(malloc(numberOfWords * sizeof(int)));
  14. calculateLengthOfWords(text,separator,wordsLength);
  15.  
  16. char** words=(char**)malloc(numberOfWords * sizeof(char*));
  17. for(int i=0; i<numberOfWords-1; ++i)
  18. {
  19.  
  20. }
  21. cout<<numberOfWords;
  22. }
  23. int countWords(char* text, char separator)
  24. {
  25. int i=0;
  26. int counter=1;
  27. while(text[i]!='\n')
  28. {
  29. if(text[i]==separator)
  30. ++counter;
  31. ++i;
  32. }
  33. return counter;
  34. }
  35. void calculateLengthOfWords(char* text, char separator, int* wordsLength)
  36. {
  37. int i=0;
  38. int wordIndex=0;
  39. int lastWordPosition=0;
  40. while(text[i]!='\n')
  41. {
  42. if(text[i]==separator)
  43. {
  44. lastWordPosition=i-lastWordPosition-1;
  45. wordsLength[wordIndex]=lastWordPosition;
  46. cout<<lastWordPosition;
  47. }
  48. ++i;
  49. }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement