Archive for the ‘String’ Category
- In: Array | C/C++ | String
- Leave a Comment
bool IsUniqueChars(const string &str) { if(str.length() > 256) return false; bool char_set[256]; for (unsigned int i = 0; i < 256; i++) { char_set[i] = false; } for (unsigned int i = 0; i < str.length(); i++) { int val = str.at(i); if(char_set[val]) return false; char_set[val] = true; } return true; }
- In: Array | C/C++ | String
- Leave a Comment
void ReverseStringWithArrayScript(char* str) { char* end = str; char tmp; int length = 0; if(str) { while (*end++) { // find the end of str and length ++length; } --end; // set one char back, since last char is null for(int i = 0, j = length - 1; i < (length / 2); i++, j--) { tmp = str[i]; str[i] = str[j]; str[j] = tmp; } // end for loop.... } // end nullptr checking loop.... } void ReverseString(char* str) { char* end = str; char tmp; if(str) { while (*end) { // find the end of str ++end; } --end; // set one char back, since last char is null while (str < end) { tmp = *str; *str++ = *end; *end-- = tmp; } // end while loop.... } // end nullptr checking loop.... }
Wild-Card Comparison
Posted May 13, 2009
on:- In: Array | C/C++ | String
- Leave a Comment
Sometimes we have needed to compare a Wild-Card string in a given string. Here is a handy function for doing this:
int WildCmp(const TCHAR* wild, const TCHAR* string) { const TCHAR *cp = NULL, *mp = NULL; while ((*string) && (*wild != ‘*’)) { if ((*wild != *string) && (*wild != ‘?’)) { return 0; } wild++; string++; } while (*string) { if (*wild == ‘*’) { if (!*++wild) { return 1; } mp = wild; cp = string+1; } else if ((*wild == *string) || (*wild == ‘?’)) { wild++; string++; } else { wild = mp; string = cp++; } } while (*wild == ‘*’) { wild++; } return !*wild; }
Tags: Wild-Card Comparison