Nessuna descrizione

psfunctions.cpp 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "psfunctions.h"
  2. /// \file
  3. ///
  4. /// \fn unsigned int countCharsOfType(const string &st, int (* filterFunction)(int args) , int fromIdx = 0, int toIdx = -1)
  5. /// \~English
  6. /// \brief Given a string will return the number of characters
  7. /// of a certain type.
  8. /// \param st string to analyze
  9. /// \param filterFunction a function pointer to a function that returns 1
  10. /// when a character of the type is given. For example, if function is islower,
  11. /// then countCharsOfType will count lowercase characters.
  12. /// \param fromIdx index from where to start count
  13. /// \param toIdx last index for count
  14. /// \~Spanish
  15. /// \brief Dada una cadena de caracters devuelve el numero de caracteres de un
  16. /// cierto tipo.
  17. /// \param st Cadena de caracteres a analizar
  18. /// \param filterFunction Apuntador a funcion a una funcion que devuelve 1
  19. /// cuando un caracter de el typo es dado. Por ejemplo, si la funciones la funcion
  20. /// islower, entonces countCharsOfType contara caracteres en minuscula
  21. /// \param fromIdx indice de desde donde comenzar a contar.
  22. /// \param toIdx ultimo indice para contar.
  23. unsigned int countCharsOfType(const string &st,
  24. int (* filterFunction)(int args) ,
  25. int fromIdx = 0, int toIdx = -1) {
  26. unsigned int ctr = 0;
  27. unsigned int len = st.length();
  28. // set the last index, -1 is the default value,
  29. // which will be interpreted as the last index.
  30. if (toIdx == -1) toIdx = len - 1;
  31. //cout << "fromIdx: " << fromIdx << " toIdx:" << toIdx << endl;
  32. // if length or indices are invalid return a 0
  33. if ( len == 0 || fromIdx < 0 || fromIdx > static_cast<int>(len - 1) ||
  34. fromIdx > toIdx || toIdx < 0 || toIdx > static_cast<int>(len - 1) )
  35. return 0;
  36. for(int i = fromIdx; i <= toIdx; i++) {
  37. if ( filterFunction( st[i]) )
  38. ctr++;
  39. }
  40. return ctr;
  41. }
  42. /// \fn unsigned int countUppercase(const string &st)
  43. /// \~English
  44. /// \brief Given a string will return the number of uppercase
  45. /// characters.
  46. /// \param st string by reference to analyze
  47. /// \~Spanish
  48. /// \brief Dada una cadena de caracteres devolvera el numero de caracteres
  49. /// en mayuscula.
  50. /// \param st cadena de caracteres por referencia a analizar.
  51. unsigned int countUppercase(const string &st) {
  52. return countCharsOfType(st, isupper);
  53. }
  54. /// \fn unsigned int countLowercase(const string &st)
  55. /// \~English
  56. /// \brief Given a string will return the number of lowercase
  57. /// characters.
  58. /// \param st string by reference to analyze
  59. /// \~Spanish
  60. /// \brief Dada una cadena de caracteres devolvera el numero de caracteres
  61. /// en minuscula.
  62. /// \param st cadena de caracteres por referencia a analizar.
  63. unsigned int countLowercase(const string &st) {
  64. return countCharsOfType(st, islower);
  65. }
  66. /// \fn int isSymbol(int c)
  67. /// \~English
  68. /// \brief Returns 1 if the passed argument is a symbol.
  69. /// \param c the character to be analyzed.
  70. /// \~Spanish
  71. /// \brief Devuelve 1 si el argumento es un simbolo.
  72. /// \param c el caracter a ser analizado.
  73. int isSymbol(int c) {
  74. return ( !islower(c) && !isupper(c) && !isdigit(c) );
  75. }
  76. /// \fn int isDigitOrSymbol(int c)
  77. /// \~English
  78. /// \brief Returns 1 if the passed argument is a digit or symbol.
  79. /// \param c the character to be analyzed.
  80. /// \~Spanish
  81. /// \brief Devuelve 1 si el argumento es un digito o simbolo.
  82. /// \param c el caracter a ser analizado.
  83. int isDigitOrSymbol(int c) {
  84. return ( isdigit(c) || isSymbol(c) );
  85. }
  86. /// \fn unsigned int countDigits(const string &st)
  87. /// \~English
  88. /// \brief Given a string will return the number of digits.
  89. /// \param st string by reference to analyze
  90. /// \~Spanish
  91. /// \brief Dada una cadena de caracteres devolvera el numero de digitos.
  92. /// \param st cadena de caracteres por referencia a analizar.
  93. unsigned int countDigits(const string &st) {
  94. return countCharsOfType(st, isdigit);
  95. }
  96. /// \fn unsigned int countSymbols(const string &st)
  97. /// \~English
  98. /// \brief Given a string will return the number of symbols.
  99. /// \param st string by reference to analyze
  100. /// \~Spanish
  101. /// \brief Dada una cadena de caracteres devolvera el numero de simbolos.
  102. /// \param st cadena de caracteres por referencia a analizar.
  103. unsigned int countSymbols(const string &st) {
  104. return countCharsOfType(st, isSymbol);
  105. }
  106. /// \fn string toUpperString(const string &st)
  107. /// \~English
  108. /// \brief Returns an uppercase version of the received string.
  109. /// \param st string by reference to analyze
  110. /// \~Spanish
  111. /// \brief Devuelve una version en mayusculas de la cadena de caracteres recibida.
  112. /// \param st cadena de caracteres por referencia a analizar.
  113. string toUpperString(const string &st) {
  114. string res = "";
  115. for (unsigned int i = 0; i < st.length(); i++)
  116. res.push_back(toupper(st[i]));
  117. return res;
  118. }
  119. /// \fn unsigned int middleDigitsOrSymbols(const string &st)
  120. /// \~English
  121. /// \brief Returns the number of digits and symbols
  122. /// that are not the first or last characters of the received string.
  123. /// \param st string by reference to analyze
  124. /// \~Spanish
  125. /// \brief Devuelve el numero de digitos y simbolos que no son el primer
  126. /// o el ultimo caracter de la cadena recibida.
  127. /// \param st cadena de caracteres por referencia a analizar.
  128. unsigned int middleDigitsOrSymbols(const string &st) {
  129. return countCharsOfType(st, isDigitOrSymbol, 1, st.length() - 2);
  130. }
  131. ///
  132. /// \fn unsigned int countCharsOfType(const string &st, int (* filterFunction)(int args) , int fromIdx = 0, int toIdx = -1)
  133. /// \~English
  134. /// \brief Given a string will return the number of characters
  135. /// of a certain type that follow a character of that same type.
  136. /// \param st string to analyze
  137. /// \param filterFunction a function pointer to a function that returns 1
  138. /// when a character of the type is given. For example, if function is islower,
  139. /// then countConsecutive will count consecutive lowercase characters.
  140. /// \~Spanish
  141. /// \brief Dada una cadena de caracters devuelve el numero de caracteres de un
  142. /// cierto tipo que sigue un caracter de el mismo tipo.
  143. /// \param st Cadena de caracteres a analizar
  144. /// \param filterFunction Apuntador a funcion a una funcion que devuelve 1
  145. /// cuando un caracter de el typo es dado. Por ejemplo, si la funciones la funcion
  146. /// islower, entonces countConsecutive contara caracteres consecutivos en minuscula
  147. unsigned int countConsecutive(const string &st,
  148. int (* filterFunction)(int args) ) {
  149. // prevSameType will be made true whenever we find a caracter
  150. // of the type we are looking.
  151. bool prevSameType = false;
  152. unsigned int ctr = 0;
  153. for(unsigned int i = 0; i < st.length(); i++) {
  154. if ( filterFunction( st[i]) ) {
  155. if (prevSameType) ctr++;
  156. prevSameType = true;
  157. }
  158. else
  159. prevSameType = false;
  160. }
  161. return ctr;
  162. }
  163. /// \fn unsigned int consecUppercase(const string &st)
  164. /// \~English
  165. /// \brief Given a string will return the number of
  166. /// uppercase characters that follow a character of that same type.
  167. /// \param st string by reference to analyze
  168. /// \~Spanish
  169. /// \brief Dada una cadena devolvera el numero de caracteres en mayuscula que
  170. /// sigue a un caracter del mismo tipo.
  171. /// \param st cadena de caracteres por referencia a analizar.
  172. unsigned int consecUppercase(const string &st) {
  173. return countConsecutive(st, isupper);
  174. }
  175. /// \fn unsigned int consecLowercase(const string &st)
  176. /// \~English
  177. /// \brief Given a string will return the number of
  178. /// lowercase characters that follow a character of that same type.
  179. /// \param st string by reference to analyze
  180. /// \~Spanish
  181. /// \brief Dada una cadena devolvera el numero de caracteres en minuscula que
  182. /// sigue a un caracter del mismo tipo.
  183. /// \param st cadena de caracteres por referencia a analizar.
  184. unsigned int consecLowercase(const string &st) {
  185. return countConsecutive(st, islower);
  186. }
  187. /// \fn unsigned int consecDigits(const string &st)
  188. /// \~English
  189. /// \brief Given a string will return the number of
  190. /// digits that follow a digit.
  191. /// \param st string by reference to analyze
  192. /// \~Spanish
  193. /// \brief Dada una cadena devolvera el numero de digitos que
  194. /// sigue a un digito.
  195. /// \param st cadena de caracteres por referencia a analizar.
  196. unsigned int consecDigits(const string &st) {
  197. return countConsecutive(st, isdigit);
  198. }