Return the 1st non-repeated character: Ex."aaaaaaak47" return 'k'
char find_first_nonrepeat(char *str)
{
char *c1 = str;
char pre_c = *c1;
while (*c1 != '\0' && *c1 == pre_c)
{
pre_c = *c1; // If the same as previous char,
c1++; // add c1 by 1 and go to next char
}
if (*c1 == '\0') // Be careful the special case!
return 0; // => all chars are the same
else
return *c1; // This is to return a char, not pointer
}
Convert an Integer to a String
char* num2str(int num, char *str)
{
char *c, *c2;
for (c = str; num; num/=10, c++)
*c = num % 10 + '0';
*c = '\0';
c2 = c - 1;
c = str; // After that we need to reverse the string,
// since string c starts from lower digit.
while (c < c2) {// It is a different way to reverse a string
*c ^= *c2;
*c2 ^= *c;
*c++ ^= *c2--;
}
return str;
}
Compare 2 strings. If equal return 1, else return 0
int Same(char *s1, char *s2)
{
char *c1, *c2;
c1 = s1;
c2 = s2;
while (*c1 != '\0' && *c2 != '\0' && *c1 == *c2) {
c1++;
c2++;
}
if (*c1 == '\0' && *c2 == '\0')
return 1; // If while loop goes to the end => equal
else
return 0;
}
A collection of embedded system and firmware/software interview questions. This blog will help you prepare for your interview efficiently.
2011年1月9日 星期日
String (2)
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言