HCL Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
dude, this gives word reversal. If you push entire words into stack (words as a whole), how can u get string reversal?? eg
How are you
stack: you (top)
are
How
void reverse(char* sta,char* end)
{
while(sta<=end)
{
char c = *sta;
*sta = *end;
*end = c;
sta++;
end--;
}
}
void reverseWords()
{
char str[MAX_LEN];
printf("Enter the string: ");
fgets(str,MAX_LEN,stdin);
char *sta,*end;
int len = strlen(str);
sta = str;
end = str+strlen(str)-2;
*(end+1) = '\0';
reverse(sta,end);
sta = strtok(str," ");
while(1)
{
end = sta+strlen(sta)-1;
reverse(sta,end);
sta = strtok(NULL," ");
if(sta == NULL)
break;
}
for(int i=0;i<len;i++)
printf("%c",str[i]);
printf("\n");
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Class4
{
static void Main(string[] args)
{
String s = "Welcome to India";
char[] c = new char[s.Length];
int count = 0;
int j = 0;
int a = 0;
for (int i = s.Length-1; i >= 0; i--)
{
count++;
if (s[i] == ' '||i==0)
{
if (i == 0)
j = i;
else
j = i+1;
while (count > 1)
{
c[a] = s[j];
j++;
a++;
count--;
}
c[a] = ' ';
count = 0;
a++;
}
}
for (int i = 0; i < c.Length; i++)
Console.Write(c[i]);
Console.ReadLine();
}
}
}
bad coding but works
int j=0; //global counter
void foo(char *s, char *t)
{
if(*s == '\0')
return;
foo(++s, t);
if(*s)
t[j++] = *s;
}
char sum[100];
int main()
{
char t[100];
int i=0, k=0;
int count=0;
char string[] = {"wel to india is goign and I am anish"};
t[strlen(string)-1] = string[0];
foo(string, t);
t[++j] = '\0';
strcpy(string, t);
memset(string, 0, strlen(string));
j=0;
for(i=0;1;) {
if(*(t+i) == '\0') {
string[k] = '\0';
sum[j+strlen(string)-1] = string[0];
foo(string, sum);
sum[++j] = '\0';
break;
} else if(!isspace(*(t+i)))
string[k++] = *(t+i);
else if(isspace(*(t+i))){
string[k] = '\0';
k=0;
sum[j+strlen(string)-1] = string[0];
foo(string, sum);
sum[++j] = ' ';
j++;
}
i++;
}
printf("%s\n", sum);
return 0;
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace testApplication
{
class Program
{
static void Main(string[] args)
{
string _name = "test string to test reverse order";
string[] splittedString = _name.Split(' ');
Console.WriteLine(_name);
for (int index = splittedString.Length-1; index >= 0; index--)
{
Console.Write(splittedString[index]+" ");
}
Console.ReadLine();
}
}
}
{
class RevStr{
public void reverseString(String str){
Map<Integer,String> map = new LinkedHashMap<Integer, String>();
StringTokenizer tok = new StringTokenizer(str, " ");
int i = 1;
while(tok.hasMoreTokens()){
map.put(i, tok.nextToken());
i++;
}
String temp = "";
for(i = 0; i < map.size(); i++ ){
temp = temp + map.get((map.size()-i)) + " ";
}
System.out.println(temp);
}
public static void main(String[] args) {
reverseString("Welcome to India");
}
}
}
public void reverseString(char []str){
int k = 0;
int l = k;
char ch;
for(int i = 0; i < str.length; i++){
if(str[i] == ' ' || str[i] == '\0'){
for(int j = i - 1; j>=(i+k)/2; j--){
ch = str[j];
str[j] = str[l];
str[l] = ch;
l++;
}
str[i] = ' ';
k = i + 1;
l = k;
}
if( i == (str.length - 1)){
for(int j = i + 1 - 1; j>=(i + 1 + k)/2; j--){
ch = str[j];
str[j] = str[l];
str[l] = ch;
l++;
}
}
}
k = 0;
char []str2 = new char[str.length];
for(int m = str.length -1 ; m >=0; m--)
str2[m] = str[k++];
String str1 = new String(str2);
System.out.println(str1);
}
#include <stdio.h>
void reverse(char *str,int begin,int end)
{
char temp;
int b,e;
b = begin;
e = end;
if(str==NULL)
return;
for(;b<e;b++,e--)
{
temp = str[e];
str[e] = str[b];
str[b] = temp;
}
}
void reverseEveryWord(char *str)
{
int temp=0,b=0;
char *start=str;
if(start==NULL)
return ;
while(*start)
{
if(*start!=' ')
{
temp++;
start++;
}else
{
reverse(str,b,temp-1);
temp++;
b = temp;
start++;
}
}
reverse(str,b,temp-1);
}
int main()
{
char str[100];
int length;
printf("Input a string please:\n");
while(gets(str)!=EOF)
{
length = strlen(str);
reverse(str,0,length-1);
reverseEveryWord(str);
printf("After Worlds Reverse:\n%s\n",str);
printf("Input a string please:\n");
}
return 0;
}
#include<stdio.h>
#include <string.h>
void reverse(char *inp_str)
{
char * pch=strrchr(inp_str, ' ');
int position = 0;
if ( pch!=NULL && *pch != '\0' ){
position = pch-inp_str;
while (pch!=NULL && *pch != '\0'){
pch++;
printf ("%c", *pch);
}
printf (" ");
inp_str[position] = '\0';
reverse(inp_str);
} else {
printf ("%s\n", inp_str);
}
}
main()
{
char inp_str[] = "Welcome to India";
reverse(inp_str);
return 0;
}
1. reverse the whole string.
2. reverse the words in this string
#include <iostream>
#include <new>
#include <cstdlib>
#include <cstdio>
#include <string>
using namespace std;
void reverse(string &s, int st, int end);
int main()
{
string s = "Welcome to Bangladesh";
int i, j = s.length()-1;
i = 0;
//cout<<s<<endl;
reverse(s, i, j);
cout<<s<<endl;
int st, end;
bool started = false;
for(i = 0; i<=j; i++){
if(isalpha(s[i]) && started == false){
started = true;
st = i;
cout<<"s[i] = "<<s[i]<<endl;
}
if(s[i] == ' '){
if(started){
end = i;
reverse(s, st, end-1);
cout<<"temporary after reverse: "<<s<<endl;
started = false;
}
}
}
if(started){
reverse(s, st, i-1);
}
cout<<"Reversed words "<<s<<endl;
return 0;
}
void reverse(string &s, int st, int end){
char temp;
int i, j;
cout<<s<<"st="<<st <<", end= "<<end<<endl;
while(st<end){
temp = s[st];
s[st] = s[end];
s[end] = temp;
st ++;
end --;
}
cout<<s<<endl;
}
in C:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void reverse(char *s, int st, int end);
int main()
{
char s[10000];// = {"Welcome to Bangladesh"};
scanf("%[^\n]s", s);
int n = strlen(s);
int i = 0;
reverse(s, i, n-1);
int st, end;
int started = 0;
for(i = 0; i<=n-1; i++){
if(isalpha(s[i]) && started == 0){
started = 1;
st = i;
}
if(s[i] == ' '){
if(started){
end = i;
reverse(s, st, end-1);
started = 0;
}
}
}
if(started){
reverse(s, st, i-1);
}
printf("Reversed words %s\n",s);
return 0;
}
void reverse(char *s, int st, int end){
char temp;
printf("s = %s st=%d end = %d\n", s, st, end);
while(st<end){
temp = s[st];
s[st] = s[end];
s[end] = temp;
st ++;
end --;
}
printf("%s\n", s);
}
Well this's a easy question to ask..
- coding.arya May 12, 2013Algorithm is:
1. Reverse the whole string.
2. Reverse the individual words.
For example:
Welcome to India
after step 1 => aidnI ot emocleW
after step 2 => India to Welcome
...
0(n) and not extra memory... :)