String Extra

convert between str and int

int StrToInt (char str[])
{
int result = 0;
int i = 0;

if(str[i])
{
result = result *10+ (str[i]-'0');
i++;
}
return result;
}

void IntToStr(int num, char str[])
{
IntToStr_Rec(num,str);
}

int count = 0;
int result = 0;
void IntToStr_Rec(int num, char str[])
{
if(num >10)
IntToStr(num/10,str);

str[count]='0'+num-result*10;

result = result *10 +num;
count ++;
}

void IntToStr(int num, char str[])
{

while(num)
{
str[i] = num % 10;
i++;
num = num/10;
}

// then reverse str;
}

suffix array
we first create suffix array, then sort the array and finally compare neighbor elements in the array.

permutation

void DoPermute(char in[], char out [] , int used[], 
int length, int recursLev) 
{ 
  int i ; 
 
  /* Base case */ 
  if (recursLev == length) { 
    printf("%s\n", out); /* print permutation */ 
    return; 
 } 
 /* Recursive case */ 
 for (i = 0; i < length; i++) 
 { 
    if (used[i]) /* if used, skip to next letter */ 
      continue; 
 
    out[recursLev] = in[i]; /* put current letter in output */ 
    used[i] =1 ; /* mark this letter as used */ 
    DoPermute(in, out, used, length, recursLev + 1); 
    used[i] =0 ; /* unmark this letter */ 
 } 
}

combinations of string "wxyz"

w        x      y    z 
wx      xy    yz 
wxy    xyz 
wxyz   xz 
wxz 
wy 
wyz 
wz
void DoCombine (char in[], char out [] , int length, int recursLev, int start) 
{ 
  int i ; 
  for (i = start; i < length; i++) { 
    out[recursLev] = in[i]; /* select current letter */ 
    out[recursLev + 1] = '\o' ; /* tack on NUL for printf */ 
    printf("%s\n", out); 
 
    if (i < length - 1) /* recurse if more letters in input */ 
        DoCombine(in, out, length, recursLev +1 , i + 1); 
  } 
}

print phone words

public static void doPrintTelephoneWords(int [] phoneNum, int curDigit, char[] result)
    {
        if(curDigit == PHONE_NUMBER_LENGTH){
            System.out.println(new String(result));
            return;
        }
 
        for(int i = 1; i <=3; i++){
            result[curDigit] = getCharKey(phoneNum[curDigit], i);
            doPrintTelephoneWords(phoneNum, curDigit + 1, result);
            if(phoneNum[curDigit] == 0 || phoneNum[curDigit] == 1)
                return;
        }
    }
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License