Interview Question
Country: India
Interview Type: In-Person
#include<stdio.h>
#include<string.h>
//Reversing the string in a special way
void reverseString( char string[] ){
int len,i; char temp;
len = strlen(string);
for(i=0;i<len/2;i++){
temp = string[i];
string[i] = string[len-i-1];
string[len-i-1]= temp;
}
}
void reverseWord(char *ptr, int wordLen){
ptr =ptr-1;
while(wordLen>0){
printf("%c",*ptr);
ptr=ptr-1;
wordLen--;
}
printf(" ");
}
void specialReverse( char string[]){
reverseString(string);
//Now that we have reverse the whole string
char *ptr; int numChar=0;
ptr=string;
printf("");
while( *ptr !='\0'){
while(( *ptr !='\0' ) &&(*ptr !=' ')){
ptr = ptr+1;
numChar++;
}
reverseWord(ptr,numChar);
numChar=0;
ptr=ptr+1;
}
}
main(){
char string[150];
printf("Please enter the sentence for special reversal: ");
scanf("%[^\n]", string);
printf("\nYou have entered \" %s \" \n",string);
specialReverse(string);
return 0;
}
#include<stdio.h>
#include<string.h>
//Reversing the string in a special way
void reverseString( char string[] ){
int len,i; char temp;
len = strlen(string);
for(i=0;i<len/2;i++){
temp = string[i];
string[i] = string[len-i-1];
string[len-i-1]= temp;
}
}
void reverseWord(char *ptr, int wordLen){
ptr =ptr-1;
while(wordLen>0){
printf("%c",*ptr);
ptr=ptr-1;
wordLen--;
}
printf(" ");
}
void specialReverse( char string[]){
reverseString(string);
//Now that we have reverse the whole string
char *ptr; int numChar=0;
ptr=string;
printf("");
while( *ptr !='\0'){
while(( *ptr !='\0' ) &&(*ptr !=' ')){
ptr = ptr+1;
numChar++;
}
reverseWord(ptr,numChar);
numChar=0;
ptr=ptr+1;
}
}
main(){
char string[150];
printf("Please enter the sentence for special reversal: ");
scanf("%[^\n]", string);
printf("\nYou have entered \" %s \" \n",string);
specialReverse(string);
return 0;
}
public static string ReverseString(string OriginalString)
{
Stack<string> myStack = new Stack<string>();
string strBuff = "";
for (int i = 0; i < OriginalString.Length; i++)
{
if (!OriginalString[i].Equals(' '))
{
strBuff += OriginalString[i];
}
else
{
myStack.Push(strBuff);
strBuff = "";
}
}
myStack.Push(strBuff);
strBuff = "";
do
{
strBuff += myStack.Pop();
strBuff += " ";
} while (myStack.Count > 0);
return strBuff;
}
String reverse(String s){
- jose September 20, 2012String[] terms = s.split("[ ]+");
String result="";
for(String term:terms){
result = term+" "+result;
}
return result;
}