Epic Systems Interview Question
Software Engineer / DevelopersString sentence = "This is great";
String words[] = sentence.split(" ");
for(int i=words.length; i<0; i++){
System.out.println(words[i]);
}
#include<iostream>
void reverse(char a[], int i,int j)
{
char temp;
while(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
i++;
j--;
}
}
int main()
{
char a[]="Hello World";
int i=0;
int j=strlen(a);
reverse(a,i,j-1);
printf("Reversed String is %s\n",a);
j=0;
while(a[i]!='\0')
{
if(a[j]==' ' || a[j]=='\0')
{
reverse(a,i,j-1);
i=j+1;
j=i;
}
else
{
j++;
}
}
printf("Reversed String is %s\n",a);
system("PAUSE");
}
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string inp;
vector<string> final;
while(cin.get()!='\n')
{
cin>>inp;
final.push_back(inp);
}
vector<string>::reverse_iterator ii;
for(ii=final.rbegin();ii!=final.rend();ii++)
cout<<*ii<<"\t";
system("PAUSE");
return 0;
}
#include<stdio.h>
#include<string.h>
main()
{ int i,j,l;
char str[59];
printf("Enter the string\n");
gets(str);
l=strlen(str);
for(i=l-1; i>=0; i--)
{ if(str[i]==' ')
{ for(j=i+1; j<l; j++)
printf("%c",str[j]);
printf(" ");
l=i;
}
if(i==0)
{ printf(" ");
for(j=0; j<l; j++)
printf("%c",str[j]);
}
}
}
public class ReverseWords {
public static void reverseWords(String words){
if(words.length() == 0 || words == null){
return;
}
String[] words_array = words.split(" ");
for(int i=words_array.length - 1;i>=0;i--){
System.out.print(words_array[i]+" ");
}
System.out.println();
}
/**
* @param args
*/
public static void main(String[] args) {
reverseWords("This is a word");
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class Reverse {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
ArrayList<String> al = new ArrayList<String>();
System.out.println("Enter the String you want to reverse");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str = br.readLine();
StringTokenizer st = new StringTokenizer(str);
while(st.hasMoreTokens())
{
al.add(st.nextToken());
}
Collections.reverse(al);
for(int i=0;i<al.size();i++)
{
System.out.print(al.get(i));
}
}
}
Here is a working code in C++. Basically I have reversed the alphabets for each word and then finally reversed the entire string.
#include <cstdio>
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char str[100], ch;
int i, len, st, ub;
scanf("%[^\n]s", str);
len=strlen(str);
cout<<"\n";
st=0;
for (i=0; i<len; i++)
{
if (str[i]==' ')
{
ub=i-1;
while (ub>=st)
{
ch=str[st];
str[st]=str[ub];
str[ub]=ch;
ub--;
st++;
}
st=i+1;
}
}
ub=len-1;
while (ub>=st)
{
ch=str[st];
str[st]=str[ub];
str[ub]=ch;
ub--;
st++;
}
ub=len-1;
st=0;
while (st<=ub)
{
ch=str[st];
str[st]=str[ub];
str[ub]=ch;
ub--;
st++;
}
for (i=0; i<len; i++)
{
cout<<str[i];
}
cout<<endl;
return 0;
}
package p1;
import java.util.ArrayList;
public class reverse_string {
public static void main(String args[])
{
String str="This is great";
ArrayList ar=new ArrayList();
int p=str.length()-1;
for(int i=p;i>=0;i--)
{
if(str.charAt(i)!=' ')
{
ar.add(str.charAt(i));
}
if(str.charAt(i)==' ' ||i==0)
{
ar.add(' ');
display(ar);
}
p=str.length()-i;
}
}
static void display(ArrayList ar)
{
for(int i=ar.size()-1;i>=0;i--)
{
System.out.print(ar.get(i));
}
ar.clear();
}
}
Better use a stack,
here is a java program.
static String reverse(String str){
StringBuilder sb = null;
if(str != null){
sb = new StringBuilder();
String arr[] = str.split(" ");
if(arr.length <=1){
return str;
}
else{
Stack<String> stack = new Stack<String>();
for(String s : arr){
stack.push(s);
}
while(!stack.isEmpty()){
sb.append(stack.pop());
sb.append(" ");
}
}
}
return sb != null ? sb.toString() : null;
}
#include<iostream>
using namespace std;
#include<string>
using std::string;
#include<stack>
void main()
{
string input;
getline(cin, input);
stack<string>mystack;
int lastspace = 0;
for (int i = 0; i < input.size() ;i++)
{
if (input.at(i) == ' ')
{
mystack.push(input.substr(lastspace, i - lastspace));
lastspace = i+1;
}
else if (i == input.size() - 1)
{
mystack.push(input.substr(lastspace, i - lastspace + 1));
}
}
while (!mystack.empty())
{
cout << mystack.top() << " ";
mystack.pop();
}
system("pause");
}
import java.util.*;
/*given a sentence reverse the sentence but not words in it
*
* for example I am a good boy - boy good a am I
* you are not allowed to use any String functions
* */
public class Solution {
public static void main(String[] args) {
String str = "I am a good boy";
reverseSentence(str.toCharArray());
}
private static void reverseSentence(char[] charArray) {
int len = charArray.length;
List<String> list = new ArrayList<String>();
int start = 0;
for (int i = 0; i < len; i++) {
if (charArray[i] == ' ') {
String reverse = "";
for (int j = start; j < i; j++) {
reverse = reverse + Character.toString(charArray[j]);
}
list.add(reverse);
start = i + 1;
}
}
System.out.println(list);
for (int i = list.size() - 1; i >= 0; i--) {
System.out.print(list.get(i) + " ");
}
}
}
- Vir Pratap Uttam May 12, 2015