McAfee Interview Question
Applications DevelopersCountry: United States
Interview Type: Written Test
#include<stdio.h>
void reverse(char *des,char *str)
{
char temp;
while(des<str)
{
temp=*des;
*des++=*str;
*str--=temp;
}
}
int main()
{
char str[]="Hello World";
printf("Original String is :: %s \n ",str);
char *temp=str;
char *dest=str;
while(*temp)
{
temp++;
if(*temp=='\0')
{
reverse(dest,temp-1);
}
//Finally Reversed String is junaT si eman yM
else if(*temp==' ')
{
reverse(dest,temp-1);
dest=temp+1;
}
//Finally Reversed String is yM eman si junaT
}
reverse(str,temp-1);
//Finally Reversed String is Tanuj is name My
printf("Finally Reversed String is :: %s",str);
//printf("Helo......");
getchar();
return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
string s("Hello World sdf4 45234");
reverse(s.begin(), s.end());
int start = 0;
while(true) {
int index = s.find(" ", start);
if (index == s.npos) {
reverse(s.begin() + start, s.end());
break;
}
reverse(s.begin() + start, s.begin() + index);
start = index + 1;
}
cout << s << endl;
return 0;
}
public static void reverse(String input)
{
String[] words = input.split(" ");
int length = words.length;
StringBuilder finalString = new StringBuilder(input.length());
for(int i=length-1;i >= 0;i--)
{
finalString.append(words[i]);
finalString.append(" ");
}
System.out.println("Result String: "+ finalString.toString());
}
Input : Hello World
Output : World Hello
what is the complexity of input.split...would it be O(n),where n is the length of the text given.....?
Yes, you are right.
Internally, it(split method) uses a 2 pass routine. In the first pass, the index of the separator characters are discovered and stored. In the second pass, the string is "split" by calling Substring repeatedly and storing the results in an output array using the indices previously saved.
As such, the algorithm is effectively O(N), since it's doing linear passes through the input string.
#include<iostream>
#include<string>
using namespace std;
void reverse(string &s)
{
int l=s.length();
char ch;
for(int i=0;i<l/2;i++)
{
ch=s[i];
s[i]=s[l-i-1];
s[l-1-i]=ch;
}
}
void str_reverse(string &s)
{
int i=0,m,l=s.length(),j;
char ch;
reverse(s);
while(i<=l)
{
j=i;
while(s[i]!=' '&&s[i]!='\0')
{
i++;
}
m=(i-j)/2;
for(int k=0;k<m;k++)
{
ch=s[j+k];
s[j+k]=s[i-1-k];
s[i-1-k]=ch;
}
i++;
}
}
int main()
{
string s;
getline(cin,s);
str_reverse(s);
//reverse(s);
cout<<s;
return 0;
}
public class StringReverseExample {
public static void main(String args[]) throws FileNotFoundException, IOException {
//original string
String str = "Sony is going to introduce Internet TV soon";
System.out.println("Original String: " + str);
//recursive method to reverse String in Java
String reverseStr = reverseRecursively(str);
System.out.println("Reverse String in Java using Recursion: " + reverseStr);
}
public static String reverseRecursively(String str) {
//base case to handle one char string and empty string
if (str.length() <= 1) {
return str;
}
return reverseRecursively(str.substring(1)) + str.charAt(0);
}
}
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
char c[20]="Hello World";
int l;
l=strlen(c);
char arr[l];
arr[0]=' ';
int i=1;
while(c[i-1]!=32)
{
arr[i]=c[i-1];
i++;
}
arr[i]='\0';
char arr1[l-i+1];
arr1[l]='\0';
while(c[l]!=32)
{
arr1[l-1]=c[l-1];
l--;
}
char *str=strcat(arr1,arr);
cout<<str;
}
#include"stdafx.h"
#include"iostream"
#include"string"
using namespace std;
#define P system("pause")
int main()
{
string s = "Hello World";
string s1, s2;
s1.assign(&s[0], &s[5]);
s2.assign(&s[6], &s[11]);
string rets = strcat( strrev(const_cast<char*>(s1.c_str())), " ");
strcat(const_cast<char*>(rets.c_str()), strrev(const_cast<char*>(s2.c_str())));
strrev(const_cast<char*>(rets.c_str()));
cout<<rets.c_str()<<endl;
P;
return 0;
}
///
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
reverseString("Hello how are you?");
Console.ReadLine();
}
private static void reverseString(string p)
{
int space = 0;
bool multiWord = false;
while (space != -1)
{
space = p.LastIndexOf(' ');
if (space != -1)
{
string wor = p.Substring(p.LastIndexOf(' '));
Console.Write(wor);
p = p.Substring(0, p.LastIndexOf(' '));
multiWord = true;
}
else
{
if (!multiWord)
{
Console.Write(p);
}
else
{
Console.Write(" " + p);
}
}
}
}
}
}
\\\
import java.io.DataInputStream;
import java.io.IOException;
public class stringreversal
{
public static void main(String args[])throws IOException
{
System.out.println("Enter the string to be reversed??");
DataInputStream in = new DataInputStream(System.in);
String abc= in.readLine();
String arr[]=abc.split(" ");
String ab;
for(int i=0,j=arr.length-1;i<=j;i++,j--)
{
ab = arr[i];
arr[i]=arr[j];
arr[j]=ab;
}
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
}
}
1) First reverse the entire string ( it gives like "dlrow olleh")
- Ravi August 16, 20132) and then call/reverse word from first character to till space encounters.
3) It gives desired output.