Qualcomm Interview Question for Software Engineer / Developers






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

Must be :

Write a function that reverses the order of the words in a string. For example, your function should transform the string “Do or do not, there is no try.” to “try. no is there not, do or Do”. Assume that all words are space delimited and treat punctuation the same as letters



void reverseWords( char str[] ){
int start = 0, end = 0, length;

length = strlen(str);
/* Reverse entire string */
reverseString(str, start, length - 1);

while( end < length ){
if( str[end] != ' ' ){ /* Skip non-word characters */

/* Save position of beginning of word */
start = end;

/* Scan to next non-word character */
while( end < length && str[end] != ' ' )
end++;
/* Back up to end of word */
end--;

/* Reverse word */
reverseString( str, start, end );
}
end++; /* Advance to next token */
}
}

void reverseString( char str[], int start, int end ){
char temp;
while( end > start ){
/* Exchange characters */
temp = str[start];
str[start] = str[end];
str[end] = temp;

/* Move indices towards middle */
start++; end--;
}
}

- dhaval0129 August 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

we can use strtok for this

#include<stdio.h>
#include<string.h>

int main(){

char str[]="hello-this is rider";
char *pch;
reverse(str);
pch=strtok(str," ,."); //we can add more spacial character

while(pch!=null){
printf("%s",pch);
pch=strtok(str," ,.")
}
}
reverse(char *str){
// code to reverse string

}

- ridercoder October 17, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

#include<stdio.h>
#include<string.h>
void reverseWords(char* a[], int n)
{
int i,j;
char* temp;
for(i=0,j=n-1;i<j;i++,j--)
{
temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}


int main(){

char str[]="hello-this is rider";
char *pch;
char* a[10];
int i=0,j;
// reverse(str);
pch=strtok(str," "); //we can add more spacial character

while(pch!=NULL){
printf("%s\n",pch);
a[i]=pch;
i++;
pch=strtok(NULL," ");
}


reverseWords(a,i);

printf("\n");

for(j=0;j<i;j++)
printf("%s\n", a[j]);
return 0;
}

- Anonymous June 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

<pre lang="c++" line="1" title="CodeMonkey39440" class="run-this">#include<iostream>
#include<string>

using namespace std;

int main()
{
string s="Pooja Chitrakar";

string s1=string(s.rbegin(),s.rend());

string s2="";
int pos;

int i=-1;

cout<<"The reverse is:"<<s1<<"\n";

while(1)
{
if(pos==-1)break;

pos=s1.find(' ',i+1);

s2=s1.substr(i+1,pos);

// cout<<s2;

cout<<string(s2.rbegin(),s2.rend())<<" ";

i=pos;
}

s2=s1.substr(i+1);

// cout<<string(s2.rbegin(),s2.rend())<<"\n";


//cout<<s2;

return 0;
}
</pre><pre title="CodeMonkey39440" input="yes">
</pre>

- Anonymous December 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

actually the question asks us to reverse the letters of each and every word in the string with the order of words remaining the same...
eg:
abcd*+/efgh##nm uv to dcba*+/hgfe##mn vu

any special symbol acts as word separator...so their order should not be changed...
the code for this problem is:

#include<iostream>
#include<string>
using namespace std;

#define max 50

void reverse(char a[]);

int main()
{
  freopen("input.txt","r",stdin);
  freopen("output.txt","w",stdout);
  char abc[max],word[max],rev[max];
  int n,j;
  cin.getline(abc,max);
  n=strlen(abc);
  j=0;
  for(int i=0;i<n;i++)
  {
      if(!((abc[i]>=65 && abc[i]<=90) || (abc[i]>=97 && abc[i]<=122)))
      {
          if(j>0)
          {
              word[j]='\0';
              reverse(word);
              strcat(rev,word);
              j=0;
          }
          rev[i]=abc[i];
          continue;
      }
      if(j==0)
         rev[i]='\0';
      word[j]=abc[i];
      j++;
  }
  if(j>0)
  {
      word[j]='\0';
      reverse(word);
      strcat(rev,word);
  }
  rev[n]='\0';
  cout<<rev<<endl;
}

void reverse(char a[])
{
    char x;
    int k=strlen(a)-1;
    for(int i=0;i<=(strlen(a)-1)/2;i++,k--)
    {
        x=a[i];
        a[i]=a[k];
        a[k]=x;
    }
}

- rban August 20, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Complete Solution is there


arungupta1.blogspot.in/2012/07/one-more-logic-reverse-string.html


please go through the link

- Arun Kumar Gupta July 21, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

reverse words: writeulearn.com/reverse-words-string-donot-reverse-word-letters/

- bhupesh January 10, 2015 | 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