Skip to content

Instantly share code, notes, and snippets.

@alexprivalov
Created January 31, 2015 19:04
Show Gist options
  • Save alexprivalov/76277c1652ea5ee3ff03 to your computer and use it in GitHub Desktop.
Save alexprivalov/76277c1652ea5ee3ff03 to your computer and use it in GitHub Desktop.
//Reverse characters in a string:
void reverse(char* s)
{
int i;
char temp;
size_t l = strlen(s);
for (i = 0; i < (l/2); ++i) {
temp = s[i];
s[i] = s[l - i];
s[l - i] = temp;
}
}
/***********************************/
void reverse(char* s)
{
int i;
size_t l = strlen(s);
for (i = 0; i < (l/2); ++i) {
s[i] ^= s[l - i];
s[l - i] ^= s[i];
s[i] ^= s[l - i];
}
}
/***********************************/
std::string foo = "born2c0de";
std::reverse(foo.begin(), foo.end());
std::cout << foo << std::endl;
//Count the number of occurrences of a specific character in a string
#include <algorithm>
#include <string>
using namespace std;
int main() {
string str("Count, the number,, of commas.");
int count = count(str.begin(), str.end(), ',');
}
//Remove non-numbers from a string
//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"
while (i != strlen (buff))
{
if ((buff[i] >= 48) && (buff[i] <= 57))
{
buff_02[j] = buff[i];
i++;
j++;
}
else
{
i++;
}
}
//Remove non-letters from a string
//if buff's content is: "15+41-2Hel54lo **1212 Wor2ld! Ho5w Are 6996 Yo7u?" then the content of buff_02 will be: "HelloWorldHowAreYou"
while (i != strlen (buff))
{
if ((buff[i] >= 65) && (buff[i] <= 90) || (buff[i] >= 97) && (buff[i] <= 122))
{
buff_02[j] = buff[i];
i++;
j++;
}
else
{
i++;
}
}
//Remove blanks from a string
//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?"
int i=0, j=0;
int len = (int)strlen(buf);
while (i != len) {
if (buff[i] != ' ')
buff[j++] = buff[i];
i++;
}
buff[j]=0;
//or using char pointer you may use the following variant:
char *i=(char*)buf, *j=(char*)buf;
do {
if (*i != ' ')
*(j++) = *i;
} while (*(i++));
//Count the number of occurrences of a specific character in a string
#include <algorithm>
#include <string>
using namespace std;
int main() {
string str("Count, the number,, of commas.");
int count = count(str.begin(), str.end(), ',');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment