Yahoo Interview Question
Software Engineer / DevelopersYou should check ((ASCII(str[i+1]) - ASCII(str[i]) == 1)
Not sure though.
Please correct if wrong.
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++;
}
}
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....
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)
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);
}
<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>
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++;
}
}
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..
// 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;
}
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());
}
}
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/>";
}
}
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));
}
}
}
}
Here is my solution in Java. Feel free to make comments.
public static void printInSequence(String str){
if(str != null){
char current = '\0', nextExpected = '\0';
StringBuilder buff = new StringBuilder();
for(int i = 0; i < str.length(); i++){
current = str.charAt(i);
if(nextExpected == '\0' || nextExpected == current){
buff.append(current);
nextExpected = (char) (current + 1);
} else {
System.out.println(buff.toString());
buff = new StringBuilder();
buff.append(current);
nextExpected = (char) (current + 1);
}
}
System.out.println(buff.toString());
}
}
Note Sure:
- Anonymous June 21, 2011But 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++]);
}