Groupon Interview Question
Software Engineer / DevelopersCountry: India
Interview Type: In-Person
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char sen[50];
char *array[40];
int loop;
clrscr();
printf("\nEnter The Date:");
gets(sen);
array[0]=(char *) strtok(sen," ");
if(array[0]==NULL)
exit(0);
for(loop=1;loop<50;loop++)
{
array[loop]=(char *) strtok(NULL," ");
if(array[loop]==NULL)
break;}
printf("The Reversed form of the given date is:");
for(--loop;loop>=0;loop--)
printf("%s ",array[loop]);
getch();
}
here am using date ... u can use string also like " hai how are u?"
it gives output as "u? are how hai"
public class ReverseString{
public static void main(String[] arg)
{
String str = "This is a String";
String finalStr="";
String[] arr = str.split(" ");
for(int i=(arr.length-1); i>=0; i--)
{
if(i==0)
finalStr += arr[i];
else
finalStr += arr[i] + " ";
}
//is name myhello
System.out.println("Reverse words =>"+finalStr);
}
}
In my opinion~ you should use string buffer instead of string to store the reversed string, because use "+=" to string need more space and time~
In my opinion~ you should use string buffer instead of string to store the reversed string, because use "+=" to string need more space and time~
public class ReverseString{
public static void main(String[] arg)
{
String str = "This is a String";
StringBuilder Finalstr=new stringbuilder();
String[] arr = str.split(" ");
for(int i=(arr.length-1); i>=0; i--)
{
if(i==arr.length-1)
finalStr.Append(arr[i])
else
finalStr.Append(" "+arr[i])
}
//is name myhello
System.out.println("Reverse words =>"+finalStr);
}
}
import java.io.*;
class careercup2
{
public static void main(String args[])throws IOException
{
String s1="";
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(isr);
System.out.println("Enter a string");
String s=in.readLine();
s=s+"";
s=""+s;
for(int i=s.length();i>=0;i--)
{
while(s.charAt(i)!=' ')
{
s1=s1+s.charAt(i);
i--;
}
continue;
}
System.out.println(s1);
}
}
include <stdio.h>
#include <string.h>
inline void swap(char *a, char *b){
char *tmp = *a;
*a = *b;
*b = tmp;
}
void reverse(char *start, char *end){
while( start < end ){
swap(start++, end--);
}
}
void reverseWords(char *str){
char *word = str;
char *tmp = str;
while(*tmp){
tmp++;
if(*tmp == '\0'){
reverse(word,tmp-1);
}
else if( *tmp == ' '){
reverse(word,tmp - 1);
word = tmp + 1;
}
}
reverse(str,tmp - 1);
}
This solution performs reversal in memory. First reverse the whole string. Then reverse the words in the reversed string.
"This is a test" => "tset a si sihT" => "test a is This".
#region Reversing the words in a string
static void Main(string[] args)
{
//This does an in memory replacement.
//First rever the whole string.
//Then reverse individual words in the string.
const string string1 = "This is a string and I am testing this";
char[] array = string1.ToArray();
int startIndex = 0;
int length = array.Length;
Console.WriteLine(array);
Reverse(array, startIndex, length);
int prevIndex = 0;
while (startIndex <= array.Length)
{
if (startIndex == array.Length)
{
Reverse(array, prevIndex, startIndex);
break;
}
if (array[startIndex] == ' ')
{
Reverse(array, prevIndex, startIndex);
startIndex++;
prevIndex = startIndex;
continue;
}
startIndex++;
}
Console.Write(array);
Console.ReadLine();
}
private static void Reverse(char[] array, int startIndex, int length)
{
bool isEven = (length - startIndex) % 2 == 0;
int mid = startIndex + (length - startIndex) / 2;
int index1 = isEven ? mid - 1 : mid;
int index2 = mid;
while (index1 >= startIndex && index2 < array.Length)
{
char temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
index2++;
index1--;
}
}
#endregion
public class MainClass1
{
public static void main(String[] args)
{
String data = "This is a program to reverse the order of words in a string";
String tempdata = "";
String rdata = "";
int old_a=-1;
int new_a=0;
for(int i = 0 ; i<3;i++)
{
while(new_a!=(-1))
{
new_a = data.indexOf(' ', (old_a+1));
if(new_a==(-1)) break;
tempdata = data.substring((old_a+1), new_a);
rdata = tempdata+ " " + rdata;
old_a = new_a;
}
}
rdata = data.substring((old_a+1), data.length())+" "+rdata;
System.out.println(rdata);
}
}
public static String reverseWordsRecursively(String original){
String [] splitStr = original.split(" ");
if(splitStr.length != 1){
int lenOfLastWord = splitStr[splitStr.length-1].length();
return splitStr[splitStr.length-1] + " ".concat(reverseWordsRecursively(original.substring(0, (original.length() - lenOfLastWord - 1))));
}else
return original;
}
// to reverse a string word by word
//for eg:arpit is---will become----is arpit
#include<stdio.h>
int main()
{
char c[30],a[30],b[30];
printf("Enter string:");
gets(a);
int s=strlen(a);
int i,j,w;
i=0;
w=0;
//(b[i]!=" ")
for(j=s;j>0;j--)
{
b[i]=a[j-1];
i++;
if(i==s)
break;
}
printf("Reversed String is:%s",b);
printf("String Reversed by each word:");
for(j=0;j<s;j++)
{
if(b[j+1]==' ' || b[j+1]==NULL)
{
for(i=j;i>=0 && (b[i]!=' ');i--)
{
//c[w]=b[i];
//w++;
//if(w>s)
//break;
printf("%c",b[i]);
}
printf(" ");
}
}
//printf("%s",c);
return 0;
}
#include <iostream>
#include <string>
//"This is a string"
//"string a is This"
using namespace std;
int main()
{
int j=0;
string s;
string copy[80];
int pos_start=0;
int pos_end=0;
cout<<"Enter the String :: ";
getline(cin,s);
for(int i=0;s[i]!='/0';i++)
{
pos_end=s.find(" ",pos_start);
if(pos_end==-1)
{
copy[j]=s.substr(pos_start,(s.length()-pos_start));
break;
}
else
{
copy[j]=s.substr(pos_start,(pos_end-pos_start));
pos_start=pos_end+1;
j++;
i=pos_end;
}
}
for(int s1=j;s1>=0;s1--)
cout<<" "<<copy[s1];
}
void reverse_words(string str)
{
int len = str.length();
for(int i = 0; i <= len / 2; ++i)
{
swap(str, i, len - i - 1);
}
int start = 0;
for(int i = 0; i < len; ++i)
{
if(i == len - 1 || (str[i] != ' ' && str[i + 1] == ' '))
{
for(int j = start; j <= (i - start) / 2; ++j)
{
swap(str, j, i - j);
}
}
if(i > 0 && (str[i] != ' ' && str[i - 1] == ' '))
{
start = i;
}
}
}
import java.util.HashMap;
import java.util.*;
// To execute Java, please define "static void main" on a class
// named Solution.
class Solution
{
public static void main(String[] args)
{
String str="This is Seattle";
String words[];
Stack<String> wordStack=new Stack<String>();
words=str.split(" ");
for(int idx=0;idx<words.length;idx++)
{
wordStack.push(words[idx]);
}
while(!wordStack.empty())
{
String buf=wordStack.pop();
System.out.println(buf);
}
}
}
In ruby space is the default option for both split and reverse.
So, The one liner is
- Uday February 12, 2013