A Journey 2 Eternity

Archive for the ‘String’ Category

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;
}
Tags: ,
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....
}
Tags: ,

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;
}

Pages

Categories

April 2024
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
2930  

Blog Stats

  • 32,515 hits