Cisco Systems Interview Question
Software Engineer / DevelopersCountry: United States
#include <stdio.h>
#include <string.h>
void reverse(char *start, char *end);
void reverse_word(char *string);
int main(){
char str[] = "Hello world";
reverse_word(str);
printf("%s\n", str);
return 0;
}
void reverse(char *start, char *end){
char tmp;
while(start <end){
tmp = *start;
*start++ = *end;
*end-- = tmp;
}
}
void reverse_word(char *string){
char *tmp = string;
char *word = string;
if(string == NULL){
printf("string cannot be empty\n");
return;
}
tmp = string;
while(*tmp){
tmp++;
if(*tmp == '\0')
reverse(word, tmp-1);
else if(*tmp == ' '){
reverse(word, tmp -1);
word = tmp+1;
}
} //reverse word by word
reverse(string, tmp-1); //reverse all char in string including space
}
#include <iostream>
using namespace std;
void reverse_by_word(char *str) {
if (*str == '\0') {
return;
}
char p[100];
int index = 0;
while (*str != ' ' && *str != '\0') {
p[index++] = *str;
++str;
}
p[index] = '\0';
reverse_by_word(++str);
cout << p << " ";
}
int main() {
char *str = "hello world";
reverse_by_word(str);
cout << endl;
}
public class ReverseWords{
public static void main(String[] args){
String s = "Hello World";
String[] s1 = s.split(" ");
String s2 = "";
for(int i=s1.length -1; i>0; i--)
if(i == s1.length -1)
s2 = s1[i];
else
s2 = s2+" "+s1[i];
System.out.println(s2);
}
}
Please can any one suggest me the some other better way to solve this problem???
public static String reverseWord(String word){
char [] ch=word.toCharArray();
reverse(ch,0,ch.length-1);
int i=0,end=0;
while (i<ch.length){
if (ch[i]!=' '){
end=i;
while (i<ch.length&&ch[i]!=' '){
i++;
}
i--;
reverse(ch,end,i);
}
i++;
}
return new String(ch,0,ch.length);
}
private static void reverse(char [] ch, int start, int end){
for (int i=start,j=end; i<j;++i,--j){
char tmp=ch[i];
ch[i]=ch[j];
ch[j]=tmp;
}
}
#include"iostream"
#include<conio.h>
char x[50] = "hello world";
void rev(int start, int end)
{
while(start < end)
{
char temp = x[start];
x[start++] = x[end];
x[end--] = temp;
}
}
int main()
{
int i,start=0,end;
strcpy(x,strrev(x));
for(i = 0 ; i <= strlen(x) ; i++)
{
if((x[i] == ' ') || (x[i] == '\0'))
{
end = i-1;
rev(start,end);
start = i+1;
}
}
printf("x = %s",x);
getch();
return(0);
}
public class Test {
//Reverse words in a sentence
public static void main(String[] args){
char temp;
char[] str = "Hello World".toCharArray();
for(int i = 0; i <= str.length/2; i++){
temp = (char) (str[i] ^ str[str.length - 1 - i]);
str[i] ^= temp;
str[str.length - 1 - i] ^= temp;
}
System.out.println(str);
int i = 0;
while(i < str.length){
int j = i;
while(j != str.length &&str[j] != ' ') j++;
for(int k = i; k <= (i + j - 1) /2; k++){
temp = (char) (str[k] ^ str[j - 1 - k + i]);
str[k] ^= temp;
str[j - 1 - k + i] ^= temp;
}
i = j + 1;
}
System.out.println(str);
}
}
#include<iostream>
#include<stack>
#include<string.h>
using namespace std;
int main(int argc, char *argv[])
{
stack <char *> words;
char str[1000],*ptr;
cout<<"Enter a string\n";
fgets(str,1000,stdin);
str[ strlen(str) -1]='\0';// to remove appended \n character...
ptr=strtok(str," ");
while( ptr!=NULL)
{
words.push(ptr);
ptr=strtok(NULL," ");
}
cout<<"Reversed string is \n";
while(words.size()!=0)
{
cout<<words.top()<<" ";
words.pop();
}
cout<<endl;
return 0;
}
reverse the whole string
- Punit Jain May 06, 2012then reverse by words.
reverse (str);
while (*str != '\0') {
start = str;
while (*str != ' ' && *str != '\0')
{
str++;
}
end = str-1;
while (start < end)
{
char c; // to swap characters
c = *start;
*start = *end;
*end = c;
start++;
end--;
}
if (*str != '\0')
str++;
}