## Yahoo Interview Question Software Engineer / Developers

• 0

Given a sequence of charecters , print consequtive sequence of charecters alone in a line, other wise print it in a new line.
eg: ABCXYZACCD
o/p :
ABC
XYZ
A
C
CD

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

Note Sure:

But this is what i think it should be

printf("%c",str[0]);
i = 1;
while(str[i]){

if (ASCII (str[i]) == ASCII(str[i+1])) // note no such thing as ASCII
{
printf("%c",str[i]);
i++;
}
else
printf("\n%c",str[i++]);

}

Comment hidden because of low score. Click to expand.
0

You should check ((ASCII(str[i+1]) - ASCII(str[i]) == 1)
Not sure though.

Comment hidden because of low score. Click to expand.
0

simple O(N^2) Algorithm is ok..but i think we can do it in O(N) ??

Comment hidden because of low score. Click to expand.
0

is it O(n^2) ???? i think it's O(n)

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

``````void increasestring(char* str)
{
char* prev = str;
while(*str)
{
if(*str-*prev != str - prev)
{
while(prev < str)
{
printf("%c", *prev);
prev++;
}
printf("\n");
}
str++;
}

while(prev < str)
{
printf("%c", *prev);
prev++;
}
}``````

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

Better solution than my previous one

``````void increasestring2(char* str)
{
char prev = 0;
while(*str)
{
if(prev==0 || *str-prev == 1)
printf("%c", *str);
else
printf("\n%c", *str);

prev = *str;
str++;
}
}``````

Comment hidden because of low score. Click to expand.
0

Wrong solution, why you put prev as 0, doesn't make sense.

Comment hidden because of low score. Click to expand.
0

To not put \n on first line

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

void increasestring2(char* str)
{
int i=1;
printf("%c",str[0]);
while(str[i])
{
if(str[i-1] + 1 != str[i])
printf("\n");
printf("%c",str[i]);
i++;
}
}

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

void print(char *s)
{
char ch= *s;
printf("%c",ch);
while(*s++!='\0')
{
if(ch == (*s -1))
{
printf("%c",*s);
ch = *s;
}
else
{
ch = *s;

printf("\n%c",*s);
}
}
}
O(n) time....

Comment hidden because of low score. Click to expand.
0

void print(char *s)
{
char ch = *s;
putchar(*s);
while(*s++ != 0){
if(ch != (*s - 1))
putchar('\n');
ch = *s;
putchar(ch);
}
}

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

In Java

``````public static void main(String args[]){

String input = "ABCPTXYUQZ";
int thisChar, prevChar;
boolean  firstChar = true;
for(int i=0;i<input.length();i++)
{
if(firstChar)
{
System.out.print(input.charAt(i));
}
else
{
thisChar = input.charAt(i);
prevChar = input.charAt(i-1);
if(thisChar != prevChar + 1){
System.out.print("\n");
}

System.out.print(input.charAt(i));
}
firstChar = false;
}

}``````

O(n)

Comment hidden because of low score. Click to expand.
0

Statement making firstChar flag as false (firstChar = false;) need to be placed in if condition instead of setting that flag to false each and every character. Else we can use index to check for the first character i.e. if( i == 0 )

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

please write the above program in c without pointer variable
and also write the program in java with simlicity

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

void Printorder(char a[],int n)
{
int i;
for(i=0;i<n;i++)
{
if(i==0)
printf("%c",a[i]);
else if(a[i]==a[i-1]+1)
printf("%c",a[i]);
else
printf("\n%c",a[i]);
}
}

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

public static void PrintSeqChar(string str)
{
if (str == null || str.Length == 0)
return;

char prv = str[0];
string line = ""+str[0];

for (int i = 1; i < str.Length; i++)
{
if (prv + 1 == str[i])
{
line += str[i];
}
else
{
Console.WriteLine(line);
line = "" + str[i];
}
prv = str[i];
}
Console.WriteLine(line);
}

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

<pre lang="" line="1" title="CodeMonkey32300" class="run-this">
public class consecChars {

public static void main(String[] args) {

String s = "ABCXYZDDDMOP";
int length = s.length();
for(int i = 0; i < length; i++) {
int t1 = Character.getNumericValue(s.charAt(i));
int t2 = Character.getNumericValue(s.charAt(i+1));
if(t2 == (t1 + 1)) {
System.out.print(s.charAt(i) + " " + s.charAt(i+1) + " ");
i++;
} else {
System.out.println(s.charAt(i));
}
}

}

}
</pre><pre title="CodeMonkey32300" input="yes">
</pre>

Comment hidden because of low score. Click to expand.
0

O(n)

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

``````void print(const char * str){
char * prev = NULL, * cur = str;
while (cur){
if(prev && *prev +1 == *cur)
printf("%c",*cur);
else{
printf("\n%c",*cur);
}
prev++; cur++;
}
}``````

Comment hidden because of low score. Click to expand.
0

Dude u r checking for forward condition ie ABC
input may consist BCA
try this input ABCZYXACCD
the output is
ABC
ZYX
A
C
CD

because its consicutive not only next consicutive..

Comment hidden because of low score. Click to expand.
0

``````Dude u r checking for forward condition ie ABC
input may consist BCA
try this input ABCZYXACCD
the output is
ABC
ZYX
A
C
CD

because its consicutive not only next consicutive...``````

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

``````This will definately work....
#include <stdio.h>

int main()
{
char str[20];
int i;
scanf("%s",&str);
printf("%c",str[0]);
for(i=1;str[i]!='\0';i++)
{
if((str[i] == str[i-1]+1) || (str[i] == str[i-1]-1))
{
printf("%c",str[i]);
}
else
{
printf("\n%c",str[i]);
}
}
return 0;
}``````

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

string str = "ABCXYZACCDL";
int start = 0;
for(int i = 0;i < str.length();)
{
if(str[i+1] == str[i] + 1)i++;
else
{
cout<<str.substr(start,i-start+1)<<endl;
i++;
start = i;
}
}

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

``````void foo(char *str)
{
int i,j;
for(i=0;str[i];++i)
{
j=i+1;
while(str[j] && str[j]==str[i]+1)
{
printf("%c",str[i]);
i++;j++;
}
printf("%c\n",str[i]);
}
}``````

Complete code here: ideone.com/okAES

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

``````#include<stdio.h>
int main()
{
char s[]="abcxyzaccd";
char a=s[0];
int i=1;

printf("%c",a);
while(s[i]!='\0')
{
if((++a)==s[i])
{printf("%c",a);i++;}
else
{printf("\n%c",s[i]);a=s[i];i++;}
}

getch();
}``````

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

// YAHOO-CAREERCUP.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<conio.h>

void print(char *x)
{
if(x!=NULL)
{
printf("%c",*x);

while(*x!='\0')
{
if(*(x+1)==*(x)+1)
{
printf("%c",*(x+1));
x++;
}
else
{
printf("\n");
printf("%c",*(x+1));
x++;
}
}
}
}

int _tmain(int argc, _TCHAR* argv[])
{
char arr[]="abcxyzaccdefxacghijklmbcd";
print(arr);
getch();
return 0;
}

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

``````public class PrintConsecutive {
public void print(char[] chars) {
for (int i = 0; i < chars.length; ++i) {
System.out.print(chars[i]);
int j = i + 1;
for (; j < chars.length; ++j) {
if (chars[j - 1] + 1 == chars[j]) {
System.out.print(chars[j]);
} else {
break;
}
}
System.out.println();
i = j - 1;
}
}

public static void main(String[] args) {
PrintConsecutive p = new PrintConsecutive();
p.print("ABCXYZACCD".toCharArray());
}
}``````

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

In PHP
\$string="ABCXYZGHKLMRTUXY";

for(\$i=0;\$i<strlen(\$string);\$i++)
{
\$extract=substr(\$string,\$i,1);
\$ord_value=ord(\$extract);
\$ord_value=\$ord_value+1;
\$m=\$i;
\$extract_another=substr(\$string,++\$m,1);
\$ord_value1=ord(\$extract_another);
if (\$ord_value==\$ord_value1)
{
echo \$extract;
}
if (\$ord_value!=\$ord_value1)
{
echo \$extract,"<br/>";
}
}

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

public class Strings {
public static void main(String[] args) {
String s = "ABCXYZACCDpPqrfedtsr";

System.out.print(s.charAt(0));
for (int i =1; i<s.length();i++)
{
int ascii1= s.charAt(i-1);
int ascii2= s.charAt(i);
if(ascii1==ascii2-1 || ascii1==ascii2+1)
{
System.out.print(s.charAt(i));
}
else
{
System.out.println();
System.out.print(s.charAt(i));

}

}

}

}

Name:

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

### Books

is a comprehensive book walking you through every aspect of getting a job at a top tech company, while focuses on software engineering interviews.

### 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.