#include <iostream> using namespace std; int main() { char s[] = "Battle of the Vowels: Hawaii vs. Grozny"; char r[] = "aeiou"; int *flags = new int[128]; // assumes ASCII! int src, dst; // Set flags for characters to be removed for( src = 0; src < strlen(r); ++src ) { flags[(int)r[src]] = 1; } src = 0; dst = 0; while(src<strlen(s)) { if(flags[(int)s[src]]!=1) { s[dst++] = s[src]; } src++; } delete[] flags; char* res = new char[dst+1]; strncpy(res, s, dst); res[dst] = ''; cout<<res<<endl; delete[] res; return 0; } /* int main(){ char s[] = "Battle of the Vowels: Hawaii vs. Grozny"; char r[] = "aeiou"; char* sNew = new char[strlen(s)]; int* flag = new int[128]; for (int i=0; i < strlen(r); i++){ flag[(int)r[i]]=1; } int iNew = 0; for (int i=0; i < strlen(s); i++){ if (flag[(int)s[i]]!=1){ sNew[iNew++] = s[i]; } } sNew[iNew]=''; cout<<sNew<<endl; delete[] flag; delete[] sNew; return 0; } */ //Highlighted at http://tohtml.com/cpp/
2012-05-31, 9:06 PM, Thursday
Remove Specified Characters
First Non-Repeated Character
#include <iostream> #include <string> using namespace std; // The first 128 code points (0–127) of Unicode correspond to // the letters and symbols on a standard U.S. keyboard. // C stype string is an array of char, and ended by '' automatically; // char x[]="abc"; then strlen(x)=3; // char x[]="abcdef"; then strlen(x)=3; // char x[]="abc"; then the real memory is bool isUniqueChars(string str) { bool char_set[256] = {0}; for (int i = 0; i < str.size(); i++) { int val = str[i]; if (char_set[val]) return false; char_set[val] = true; } return true; } int main() { char str[20] = "abcd", xRepeat = ' '; bool iRepeat = false; int i = 0, len = strlen(str); for (i = 0; i < len;i++) { iRepeat = false; for (int j = 0; j < len; j++) { if (j!=i && str[j]==str[i]) { iRepeat = true; break; } } if(!iRepeat) { xRepeat = str[i]; break; } } cout<<"In the string of "<<str<<endl; cout<<"the first nonrepeat char is the "<< i+1 << " th char: "<<xRepeat<<endl<<endl; cout<<isUniqueChars(str)<<endl; return 0; } //Highlighted at http://tohtml.com/cpp/ //Bred 3 + C++