HCL Interview Question
Developer Program EngineersTeam: 8
Country: India
Interview Type: In-Person
int main(int argc,WCHAR *argv[])
{
string paraGrp = "Once when a Lion was asleep, a little Mouse began running up and down upon himxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx xxxx.";
/*stringstream ss(paraGrp);
string buffer;
int count =0;
while(ss >> buffer)
{
if(buffer.length() > count)
printf("%c",buffer[count]);
else
printf("\n");
count++;
}*/
char delim= ' ';
int lastPrint=0;
int spaceCount=0;
for(int i=0;i< paraGrp.length();i++)
{
if(paraGrp[i] == delim && paraGrp[i+1] == delim)
continue;
if(paraGrp[i] == delim)
{
if(i - lastPrint < spaceCount+1)
{
printf("\n");
spaceCount++;
lastPrint = i+1;
}
else if (lastPrint+spaceCount <paraGrp.length())
{
printf("%c",paraGrp[lastPrint+spaceCount]);
spaceCount++;
lastPrint = i+1;
}
}
}
printf(" SpaceCount %d , lastPrint %d ",spaceCount,lastPrint);
}
#include <stdio.h>
#define MBUF 1024
int main()
{
char buff[MBUF] = {0,};
int n = 0;
while(fgets( buff, MBUF, stdin ) )
{
n = strlen(buff);
if( buff[n-1] == '\n' )
{
buff[n-1] = '\0';
n--;
}
char *bptr = NULL, *token = NULL, *sptr = NULL;
int count = 0;
for ( bptr = buff ;; bptr = NULL )
{
token = strtok_r( bptr, " ", &sptr );
if( token == NULL )
break;
if( strlen(token) < count )
printf("\n");
else
printf("%c", token[count]);
count++;
}
}
}
#include<stdio.h>
int main()
{
char a[50];
printf("enter the string:");
gets(a);
printnchar(a);
}
void printnchar(char *p)
{
char *s;
int i=0,c=0;
while(*p!='\0')
{
c=0;
s=p;
while((*s!=' ')&&(*s!='\0'))
{
c++;
s++;
}
if(c>i)
{
printf("%c",*(p+i));
i++;
}
else
{
printf("\n");
}
if(*s!='\0')
{
p=s+1;
}
else
{
p=s;
}
}
I think the example output is wrong if the question is right. Taking the question as right into consideration the output should be something like
Oh
o
e
l
n
.
Thus the solution in will be :
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
#define DELIM_SPACE " "
main()
{
char* curr_str, *saveptr, msg_data[200] = "Once when a Lion was asleep, a little mouse began running up and down upon him";
into i = 0;
while(NULL != (curr_str = strtok_r(msg_data, DELIM_SPACE, &saveptr))) {
int l = strlen(curr_str);
(i < l && l> 3) ? std :: court << curr_str[i++] : std:: cout << " \n";
strcpy(msg_data, saveptr);
}
}
the example is wrong, the e from mouse should not be output, instead the 3rd N from running fits the pattern.
- Woody November 22, 2013{
string inString = "Once when a Lion was asleep, a little Mouse began running up and down upon him. ";
string[] Words = inString.Split(new char[] { ' ', '.', ',', '!', '?' }, StringSplitOptions.RemoveEmptyEntries);
int EndPos = Words.Length;
int CurPnt = 1;
string WordOut = string.Empty;
for (int i = 0; i < EndPos; i++)
{
string parseword = Words[i];
if (CurPnt > parseword.Length)
{
if (WordOut.Length > 0)
{
Console.WriteLine(WordOut);
WordOut = string.Empty;
}
}
else
{
WordOut = WordOut + parseword[CurPnt - 1] + " ";
CurPnt++;
}
}
if (WordOut.Length > 0)
{
Console.WriteLine(WordOut);
WordOut = string.Empty;
}
string Oprkeys = Console.ReadKey().ToString();
}
}