Qualcomm Interview Question
Software Engineer / Developerswe 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
}
#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;
}
<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>
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;
}
}
Must be :
- dhaval0129 August 16, 2010Write 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--;
}
}