Cisco Systems Interview Question for Software Engineer / Developers


Country: United States




Comment hidden because of low score. Click to expand.
2
of 2 vote

reverse the whole string
then 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++;
}

- Punit Jain May 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

or vice-versa

- codinglearner May 09, 2012 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

# Ruby solution
def reverse(sentence)
  reversed = sentence.split(' ').reverse
  reversed.each {|x| print "#{x} "}
end

- Jason Kim May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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


}

- band May 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- milo May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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???

- java solution May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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;
}
}

- Scott May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void reverseWords(String abc){
String [] wordsArray = abc.split(" ");
StringBuffer buffer = new StringBuffer();
for(int i=wordsArray.length-1;i>=0;i--){
buffer.append(wordsArray[i]+" ");

}
System.out.println(buffer.toString());
}

- Anonymous May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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);
}

- vivekanand May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 vote

//C#
string s = "hello world";
string[] array = s.Split(new char[] { ' ' });
Array.Reverse(array);
Console.WriteLine(string.Join(" ", array));

- Petri Tuononen May 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

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);
	}
}

- Lynn May 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

You can use python

s="Hello World"
m=s.split(" ")
k=len(m)
while k!=0:
	print m[k-1],
	k=k-1

- Terry May 22, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#!/usr/bin/perl -w

$sStr = "Hello world";
@strManip = split(/ /, $sStr);
for($i = 0; $i < scalar(@strManip); $i++) {
        print @strManip[scalar(@strManip) -$i - 1], " ";
}
print "\n";

- Anonymous May 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hmmm

- topcoder May 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Lot of code, not much explanation.....
Here is the basic idea:
Original string : "Hello World"
Reverse each word: "olleH dlroW"
Reverse entire string: "World Hello"

- collegesenior October 09, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#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;
}

- Anonymous November 11, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
void main()
{
char *p="nand";
char *h,*u;
int l=0;
while(*u!='\0')
{
*u++=*p++;
l++;
printf("%c%c\n",*p,*u );
}

printf("%c",*u);


do{
*h++ = *u++;
}while(u!=p);
printf("%c",h);

}

- Anonymous November 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Cisco gives hike once in 2-3 years.

New-Joinees only get hike after 2-3 years, so take 100% hike at the time of joining .. . otherwise don't cry after joining. :)

- Simple April 25, 2014 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More