Showing posts with label string manipulation. Show all posts
Showing posts with label string manipulation. Show all posts

Friday, December 2, 2011

A few simple logical questions !


1.) 

Multiplying a number by 7 without using * and + operators.

NewNum = Num << 3;                      // multiplied by 2^3 = 8

NewNum = NewNum - Num;             // 8 – 1 = 7

2.)

Finding the last character of any String.
                              (OR)

Write a function that finds the last instance of a character in a string


char *lastChar(char *String, char ch)
{
char *cStr = NULL;


// traverse the entire string


while(*String ++ != NULL ) 
{
if(*String==ch) 
cStr = String;
}
return cStr;


Saturday, November 26, 2011

Strange are the ways of C


Have look at the following snippet, you can see 4 values that which will be printed for each iteration of the loop when you run it. Actually they all are just different ways of representing the same idea !! Try it out on www.codepad.org to see and believe !!

main()
{
char s[ ]="man";
int i;
for(i=0;s[i];i++)
printf("\n %c %c %c %c ", s[ i ] , *(s+i) , *(i+s) , i[s] );
}