Advertisement
Guest User

Untitled

a guest
Jan 31st, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. //Reverse characters in a string:
  2. void reverse(char* s)
  3. {
  4. int i;
  5. char temp;
  6. size_t l = strlen(s);
  7.  
  8. for (i = 0; i < (l/2); ++i) {
  9. temp = s[i];
  10. s[i] = s[l - i];
  11. s[l - i] = temp;
  12. }
  13. }
  14. /***********************************/
  15. void reverse(char* s)
  16. {
  17. int i;
  18. size_t l = strlen(s);
  19.  
  20. for (i = 0; i < (l/2); ++i) {
  21. s[i] ^= s[l - i];
  22. s[l - i] ^= s[i];
  23. s[i] ^= s[l - i];
  24. }
  25. }
  26. /***********************************/
  27. std::string foo = "born2c0de";
  28. std::reverse(foo.begin(), foo.end());
  29. std::cout << foo << std::endl;
  30.  
  31. //Count the number of occurrences of a specific character in a string
  32. #include <algorithm>
  33. #include <string>
  34. using namespace std;
  35. int main() {
  36. string str("Count, the number,, of commas.");
  37. int count = count(str.begin(), str.end(), ',');
  38. }
  39.  
  40. //Remove non-numbers from a string
  41. //if buff's content is: "hello 2 wor145+ld--1! how 19521 a*re 1254y**ou?" then the content of buff_02 will be: "21451195211254"
  42. while (i != strlen (buff))
  43. {
  44. if ((buff[i] >= 48) && (buff[i] <= 57))
  45. {
  46. buff_02[j] = buff[i];
  47. i++;
  48. j++;
  49. }
  50. else
  51. {
  52. i++;
  53. }
  54. }
  55.  
  56. //Remove non-letters from a string
  57. //if buff's content is: "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?" then the content of buff_02 will be: "HelloWorldHowAreYou"
  58. while (i != strlen (buff))
  59. {
  60. if ((buff[i] >= 65) && (buff[i] <= 90) || (buff[i] >= 97) && (buff[i] <= 122))
  61. {
  62. buff_02[j] = buff[i];
  63. i++;
  64. j++;
  65. }
  66. else
  67. {
  68. i++;
  69. }
  70. }
  71. //Remove blanks from a string
  72. //if buff's content is: "he llo wo r ld! how ar e y ou?" then the content of buff_02 will be: "helloworld!howareyou?"
  73. int i=0, j=0;
  74. int len = (int)strlen(buf);
  75.  
  76. while (i != len) {
  77. if (buff[i] != ' ')
  78. buff[j++] = buff[i];
  79. i++;
  80. }
  81.  
  82. buff[j]=0;
  83.  
  84. //or using char pointer you may use the following variant:
  85. char *i=(char*)buf, *j=(char*)buf;
  86.  
  87. do {
  88. if (*i != ' ')
  89. *(j++) = *i;
  90. } while (*(i++));
  91.  
  92. //Count the number of occurrences of a specific character in a string
  93. #include <algorithm>
  94. #include <string>
  95. using namespace std;
  96. int main() {
  97. string str("Count, the number,, of commas.");
  98. int count = count(str.begin(), str.end(), ',');
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement