Advertisement
Yanislav29

Untitled

Dec 23rd, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. // ConsoleApplication2.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. //
  3.  
  4.  
  5. #include <iostream>
  6.  
  7.  
  8. using namespace std;
  9.  
  10.  
  11. //podgotovka za kontrolno
  12. bool isLetter(char c) {
  13. return ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
  14.  
  15. }
  16. bool isUpper(char c) {
  17. return ((c >= 'A' && c <= 'Z'));
  18. }
  19. bool isLower(char c) {
  20. return (c >= 'a' && c <= 'z');
  21. }
  22. int capital(char* str) {
  23. for (size_t i = 0; str[i] != '\0'; i++)
  24. {
  25. if (i == 0 && isLower(str[i])) {
  26. str[i] = str[i] ^ ' ';
  27. }
  28. if (i != 0 && !isLetter(str[i - 1]) && isLower(str[i])) {
  29. str[i] = str[i] ^ ' ';
  30. }
  31. else {
  32. if (isUpper(str[i]) && i != 0 && isLetter(str[i - 1])) {
  33. str[i] = str[i] ^ ' ';
  34. }
  35. }
  36. }
  37. return 0;
  38. }
  39. void longestWord(char * txt) {
  40. unsigned start = 0, max_start = 0;
  41. unsigned Cnt = 0, max_cnt = 0;
  42.  
  43. for (size_t i = 0; txt[i] != '\0'; i++)
  44. {
  45. if (i == 0 && isLetter(txt[i])) {
  46. start = i;
  47.  
  48. }
  49.  
  50. if (i != 0 && !isLetter(txt[i - 1]) && isLetter(txt[i])) {
  51. start = i;
  52. }
  53. if (!isLetter(txt[i])) {
  54. Cnt = 0;
  55. }
  56. else {
  57. Cnt++;
  58. }
  59.  
  60. if (max_cnt < Cnt) {
  61. max_cnt = Cnt;
  62. max_start = start;
  63. }
  64.  
  65. }
  66. for (size_t i = max_start; i < max_cnt + max_start; i++)
  67. {
  68. cout << txt[i];
  69. }
  70. cout << endl;
  71.  
  72. }
  73.  
  74. int main()
  75. {
  76. char str[] = "-this is a tExt.for CAPitalize... hahahahha";
  77. capital(str);
  78. cout << str << endl;
  79. cout << "The longest word is:";
  80. cout << endl;
  81. longestWord(str);
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement