Strings Interview Programs

Programs that are frequently asked in interviews for Software Engineer Position with C / C++ skills
( Mostly less than 6 years experience )

String Reversal


Using Recursive function:

// include relevant header files

void rev_str(char* s)
{
    if(*s != '\0')               // if(*s) should also work.
         rev_str(s+1);

    cout<<*(s);
}

int main()
{
   rev_str("Programming Is Easy");
}
Using XOR

//include relevant header files

char* rev(char* str)
{
  int end= strlen(str)-1;
  int start = 0;

  while( start < end )
  {
    str[start] ^= str[end];
    str[end] ^=   str[start];
    str[start]^= str[end];

    ++start;
    --end;
  }

  return str;
}

int main()
{
  char str[50]="Reversing a string using XOR";
  puts(rev(str));

}
Using Pointers :

char * strReverse(const char* str)
{
	char *front = str;             // char pointer to first character
	char *last;				 // char pointer to last character
	char ch;				 // char to hold letter temporarily

	// let last point to last character

	last = str + strlen(str) - 1;	

while(front < last)            // let pointer front move forward
	{					 // and pointer last move backward
		ch = *last;			 // one character at a time till
		*last = *front;		 // they crossover, meanwhile
		*front = ch;		 //  swap using temp variable ch.
		front++;
		last--;
	}
	return str;
} 

String Copy


void myStringCopy(char *dest, const char* source)
{
	while(*dest++ = *source++);
}

int main()
{

	const char * Source = "ABCDEFGHI";	// the source should be const
							// the destination should
							// have memory allocated.

char *destination = new char[strlen(Source) + 1];

	cout<<"Source is\t"<< Source;
	myStringCopy(destination, Source);
	cout<<"Destination string is \t"
              << destination ;
}

String Concatenation


char * myStrCat(char *dest, char *source)
{
	char *temp = new char[strlen(dest) + strlen(source) + 1];

	// Here memory is allocated for the concatenated string in temp.

	char *final;	// final points to the start of the
// concatenated string.
	final = temp;

	while(*dest)
	{
             *temp++ = *dest++;
	}

// if the assignment *temp++ = *dest++ is put in while
// condition, '\0' also gets copied to  temp. So temp would end
// here and the concatenated string would not show up.

	while(*temp++ = *source++);

	return final;
}

int main()
{
	cout<<"Concatenated string is "<<
        myStrCat(“Hello “,”World”);
}

String Compare


int strCompare(char *str1, char *str2)
{
	while(*str1 == *str2)
	{
		if(*str1 == '\0')
		{
			return 0;
		}

		str1++;
		str2++;
	}
	return (*str1 - *str2);

}

int main()
{
	char *first = "INDIA";
	char *second = "INDIa";

	if(!(strCompare(first,second)))
	{
		cout<<"Strings are equal\n";
	}
	else
	{
		cout<<"Strings are not equal\n";
	}
}

Finding whether Palindrome


int isPalindrome(char *string)
{
	char *temp = string + strlen(string) - 1;

	while(string < temp)
	{
		if(*string != *temp)
		{
			return 0;
		}
		string++;
		temp--;
	}
	return 1;
}

int main()
{
	char *palin = "PALINDROME";

	if(isPalindrome(palin))
	{
		cout<<"It is Palindrome\n";
	}
	else
	{
		cout<<"It is not palindrome\n";
	}
}





SocialTwist Tell-a-Friend