Epic Systems Interview Question


Country: United States




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

Perfect running code
Please let me know if you find any mistake or I haven't handled any case.

public class contiAlpha {
	public static void printContiAlpha(String in){
		in = in.toLowerCase();
		char prevChar = 'a';
		char currentChar = 'a';
		int i = 1;
		boolean contiguous = false;
		while(i<in.length()){
			prevChar = in.charAt(i-1);
			currentChar = in.charAt(i);
			if(currentChar==prevChar+1){
				System.out.print(prevChar);
				contiguous = true;
			}
			else{
				if(contiguous){
					System.out.print(prevChar);
					System.out.print("; ");
				}
				contiguous=false;
			}
			i++;
		}
		if(currentChar==prevChar+1) System.out.print(currentChar);		
	}
	public static void main(String[] args) {
		String in = "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
		printContiAlpha(in);
	}
}

- thepraveen0207 October 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 2 votes

You have a problem here, I think. Have you ever tested the string like this "abcdz"? Your output is "abcd;". However, I do not think you should also print ";" here.

- Justin October 10, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

if you append an invalid char, let's say '@' at the end of the string, we don't need to worry about the last char.

- albertchenyu March 04, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 2 vote

Algo :
1) check continuous chars , if they are consecutive , keep on printing them .
2) if some point the consecutive link breaks , start from the next element again . no need to go back because all the consecutive elements we have already checked & found some char which is not . So that char can't ever be considered in between a consecutive link ever .

This way we traverse the string just once & able to print the consecutive chars . So execution order is O(N).

Code for the above algo will be


void PrintCont( char * str){

int i = 0,c = 0,nextc = 0 ; char substr[100];

while(*str){
c = (int)(*str);
nextc = (int)(*(str + 1));
if(nextc && nextc - c == 1){
substr[i++] = *str;
}
elseif( i){
substr[i++] = *str;
substr[i] = '\0';
printf("%s\n" , substr);
i = 0;
}

str++;
}

}

- Saikat February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Good one !

- Sriram March 01, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Your code doesn't take into account the case when two adjacent letters are upper-case and lower-case.
For example 'aBc' should count as continuous, but would not pass your if test.

- Anonymous March 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void printConsecutive(String str){
		if(str==null || str.length()<=1)
				return;
		str = str.toLowerCase();
		StringBuffer temp = new StringBuffer();
		for(int i =0;i<str.length();i++){
			if((temp.length()==0) || ((temp.length()>0) && (str.charAt(i) - temp.charAt(temp.length()-1) == 1)))
				temp.append(str.charAt(i));
			else{
				if(temp.length()>1){
					System.out.println(temp);
				}
				temp.delete(0, temp.length());
				temp.append(str.charAt(i));
			}	
			}
		if(temp.length()>1){
			System.out.println(temp);
		}
		
		}

- loveCoding February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

gdsshtjjklsersc

- Anonymous September 01, 2021 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

char lower(char ch){
  int diff = 'A' - 'a';
  if(ch >= 'A' && ch <= 'Z')
    return ch - diff;
  else
    return ch;
}

int main(int argc, char **argv){
  char *orig = argv[1] ;

  int i = 0 ;
  bool flag = true;
  while(orig[i] != '\0'){
    if(i != 0){
      if(lower(orig[i]) == lower(orig[i-1]) + 1){
	if(flag){
	  printf("%c", orig[i-1]) ;
	  flag = false;
	}
	printf("%c", orig[i]) ;
      }
      else{
	if(!flag){
	  printf(", ") ;
	  flag = true;
	}
      }
    }
    ++i;
  }
}

- Anonymous February 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Could anyone let me knw the above program in C#

- Anonymous February 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

public static void printOutSequence(string a)
{
bool end = false;
int i = 0;
for (; i <a.Length - 1; i++)
{
if (a[i + 1] == a[i] + 1)
{
Console.Write(a[i]);
end = true;
}
else
{
if (end)
{
Console.Write(a[i] + ";");
end = false;
}
}
}

- C# February 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Could anyone let me knw the above program in C#

- Anonymous February 08, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

:)



public class Sequence {
public static void main(String args[])
{
String s="abcdekghjklmaaaahijkhdujdksdhkabcdref";
int j=0;
int u=0;
for(int i=0; i<(s.length()-1);i++)
{
j++;
char c=s.charAt(i);
char d=s.charAt(j);
if((int)(c+1)!=(int)d)
{
u=0;
continue;
}
{
if(u==0)
{
System.out.println();
System.out.print(s.charAt(i));
}
if(s.charAt(i)!=s.charAt(j-1))
{
System.out.print(s.charAt(i));
}
System.out.print(s.charAt(j));
u=1;

}
}
}

}

- Manasa V. February 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

works gr8

- varsha March 22, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void continuousAlphabetic(char[] input){
	int inLen = input.length();
	char buff = input[0];
	boolean patternFound = false;
	for (int i = 1; i < inLen; ++i){
		if (input[i] == buff + 1 || input[i] == buff + 1 - 32){
			System.out.println(buff);
			patternFound = true;
		} else {
			if (patternFound){
				System.out.println(buff + ";");
			}
			patternFound = false;
		}
		buff = input[i];
	}
}

- Suz February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I was wrong before, lack of one more condition in if...else

public static void continuousAlphabetic(char[] input){
		int inLen = input.length;
		char buff = input[0];
		boolean patternFound = false;
		for (int i = 1; i < inLen; ++i){
			if (input[i] == buff + 1 || input[i] == buff + 1 - 32 || input[i] == buff + 1 + 32 ){
				System.out.print(buff);
				if (i==inLen-1){System.out.println(input[i] + ";");}
				patternFound = true;
			} else {
				if (patternFound){
					System.out.println(buff + ";");
				}
				patternFound = false;
			}
			buff = input[i];
		}
	}

- Suz February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I was wrong before, lack of one more condition in if...else

public static void continuousAlphabetic(char[] input){
		int inLen = input.length;
		char buff = input[0];
		boolean patternFound = false;
		for (int i = 1; i < inLen; ++i){
			if (input[i] == buff + 1 || input[i] == buff + 1 - 32 || input[i] == buff + 1 + 32 ){
				System.out.print(buff);
				if (i==inLen-1){System.out.println(input[i] + ";");}
				patternFound = true;
			} else {
				if (patternFound){
					System.out.println(buff + ";");
				}
				patternFound = false;
			}
			buff = input[i];
		}
	}

- Suz February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Sorry I was wrong before, lack of one more condition in if...else

public static void continuousAlphabetic(char[] input){
		int inLen = input.length;
		char buff = input[0];
		boolean patternFound = false;
		for (int i = 1; i < inLen; ++i){
			if (input[i] == buff + 1 || input[i] == buff + 1 - 32 || input[i] == buff + 1 + 32 ){
				System.out.print(buff);
				if (i==inLen-1){System.out.println(input[i] + ";");}
				patternFound = true;
			} else {
				if (patternFound){
					System.out.println(buff + ";");
				}
				patternFound = false;
			}
			buff = input[i];
		}
	}

- Suz February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is written in python

The functional approach:

def fold(a, b):
	if a[-1] == b[0]:
		return a[:-1] + b
	else:
		return a + b

string = 'AbcDefljdflsjflmnopflsjflasjftuvWxYz'
string = string.lower()
reduce(fold,[x for x in izip(string, string[1:]) if ord(x[0])+1 == ord(x[1])])

The iterative approach :

def parse(string):
    res = []
    temp = ""
    string = string.lower()
    for i in xrange(len(string) - 1):
        if (ord(string[i]) + 1) == ord(string[i+1]):
            temp += string[i]
        else:
            temp += string[i]
            if len(temp) > 1:
                res.append(temp)
            temp = ""
    temp += string[-1]
    if len(temp) > 1:
        res.append(temp)
    temp = ""
    return res

string = 'AbcDefljdflsjflmnopflsjflasjftuvWxYz'
string = string.lower()
print parse(string)

- Gokulnath Haribabu February 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

In the iterative approach,

The string conversion to lower is taken care of inside the parse function itself. Hence string = string.lower() is not required. ( it was a copy paste mistake )

- Gokulnath Haribabu February 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <utility>
#include <string.h>
#include <map>
#include <conio.h>
using namespace std;

int main()
{
int j=0,i=0;
string a = "4678912356012356";
string temp[80];
int k=0;

for(i=0;i<16;i++,k++)
{
bool flag=false;
if(a[i]!=a[i+1]-1)
{
if(i>0)
{
temp[k]=a[i];
flag=true;
}
if(flag==true)
k++;
temp[k]=";";

}
else
{
temp[k]=a[i];
}
//k++;

}

int len=sizeof(temp)/sizeof(temp[0]);

for(k=0;k<len;k++)
cout<<temp[k];
getch();

return 0;
}

- pshankar87 February 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void printString(String str)
    {
      str = str.toLowerCase();
      StringBuffer s = new StringBuffer();
      s.append(str.charAt(0));
      for(int i =1; i< str.length();i++)
      {
        if(str.charAt(i)-str.charAt(i-1)==1)
          s.append(str.charAt(i));
        else
        {
          if(s.length()>=2) System.out.print(s+";");
          s.delete(0, s.length());  
        }
      }
      if(s.length()>=2) System.out.print(s+";");    //print the last substring
    }

- Dongqiao February 26, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The ouput should be abcdef;lmnop;tuvwxyz;

public static void main(String args[]){
String s= "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
ArrayList<String> arr=printConsecutive(s);
for(int i=0;i<arr.size();i++)
System.out.print(arr.get(i)+";");
}

private static ArrayList<String> printConsecutive(String s) {
ArrayList<String> array = new ArrayList<String>();
int beginIndex=0;
int diff='a'-'A';
for(int i=1;i<s.length();i++){
if((s.charAt(i)<='Z'&&s.charAt(i)>='A')||
(s.charAt(i)<='z'&&s.charAt(i)>='a')){
int gap =(s.charAt(i)<'a'?s.charAt(i)+diff:s.charAt(i))
-(s.charAt(i-1)<'a'?s.charAt(i-1)+diff:s.charAt(i-1));
if(i==1&&gap!=1)
beginIndex=2;
else if(gap!=1){
if(i-beginIndex!=1)
array.add(s.substring(beginIndex,i).toLowerCase());
beginIndex=i;
}
if(i==s.length()-1&&i!=beginIndex)
array.add(s.substring(beginIndex,i+1).toLowerCase());
}
}
return array;
}

- issac February 27, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think the correct output should be "abcdef;lmnop;tuvwxyz;", it must be a typo... XD

public static void contdLetters(String input){
		input = input.toLowerCase();
		char buf = ';';
		for (int i=0; i<input.length()-1; ++i){
			if (input.charAt(i+1) != input.charAt(i)+1){
				if (buf != ';'){
					System.out.print(input.charAt(i));
					System.out.print(";");
					buf = ';';
				}
				
			} else {
				System.out.print(input.charAt(i));
				buf = input.charAt(i);
			}
			
		}
		System.out.println(input.charAt(input.length()-1)+";");
	}

- dummy March 06, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What if the input is "abcghi"? does it work for your algo?

- Tiger May 02, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
int i=0, len, n;
printf("Enter the length of the string\n");
scanf("%d", &n);
char *str = (char *)malloc(sizeof(char)*(n+1));
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
str[len]='\0';
while(i<len)
{
if(str[i+1] == str[i]+1)
{
printf("%c",str[i]);
i++;
}
else
{
if(str[i+1] == '\0')
{
printf("%c;",str[i]);
i++;
}
else
{
printf("%c;",str[i]);
i++;
}
}
}
getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
      int i=0, len, n;
      printf("Enter the length of the string\n");
      scanf("%d", &n);
      char *str = (char *)malloc(sizeof(char)*(n+1));
      printf("Enter the string\n");
      scanf("%s",str);
      len = strlen(str);
      str[len]='\0';
      while(i<len)
      {
                  if(str[i+1] == str[i]+1)
                  {
                            printf("%c",str[i]);
                            i++;
                  }
                  else
                  {
                            if(str[i+1] == '\0')
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                            else
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                  }
      }
      getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
      int i=0, len, n;
      printf("Enter the length of the string\n");
      scanf("%d", &n);
      char *str = (char *)malloc(sizeof(char)*(n+1));
      printf("Enter the string\n");
      scanf("%s",str);
      len = strlen(str);
      str[len]='\0';
      while(i<len)
      {
                  if(str[i+1] == str[i]+1)
                  {
                            printf("%c",str[i]);
                            i++;
                  }
                  else
                  {
                            if(str[i+1] == '\0')
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                            else
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                  }
      }
      getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
      int i=0, len, n;
      printf("Enter the length of the string\n");
      scanf("%d", &n);
      char *str = (char *)malloc(sizeof(char)*(n+1));
      printf("Enter the string\n");
      scanf("%s",str);
      len = strlen(str);
      str[len]='\0';
      while(i<len)
      {
                  if(str[i+1] == str[i]+1)
                  {
                            printf("%c",str[i]);
                            i++;
                  }
                  else
                  {
                            if(str[i+1] == '\0')
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                            else
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                  }
      }
      getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
      int i=0, len, n;
      printf("Enter the length of the string\n");
      scanf("%d", &n);
      char *str = (char *)malloc(sizeof(char)*(n+1));
      printf("Enter the string\n");
      scanf("%s",str);
      len = strlen(str);
      str[len]='\0';
      while(i<len)
      {
                  if(str[i+1] == str[i]+1)
                  {
                            printf("%c",str[i]);
                            i++;
                  }
                  else
                  {
                            if(str[i+1] == '\0')
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                            else
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                  }
      }
      getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
      int i=0, len, n;
      printf("Enter the length of the string\n");
      scanf("%d", &n);
      char *str = (char *)malloc(sizeof(char)*(n+1));
      printf("Enter the string\n");
      scanf("%s",str);
      len = strlen(str);
      str[len]='\0';
      while(i<len)
      {
                  if(str[i+1] == str[i]+1)
                  {
                            printf("%c",str[i]);
                            i++;
                  }
                  else
                  {
                            if(str[i+1] == '\0')
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                            else
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                  }
      }
      getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
      int i=0, len, n;
      printf("Enter the length of the string\n");
      scanf("%d", &n);
      char *str = (char *)malloc(sizeof(char)*(n+1));
      printf("Enter the string\n");
      scanf("%s",str);
      len = strlen(str);
      str[len]='\0';
      while(i<len)
      {
                  if(str[i+1] == str[i]+1)
                  {
                            printf("%c",str[i]);
                            i++;
                  }
                  else
                  {
                            if(str[i+1] == '\0')
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                            else
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                  }
      }
      getch();

}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<conio.h>
main()
{
      int i=0, len, n;
      printf("Enter the length of the string\n");
      scanf("%d", &n);
      char *str = (char *)malloc(sizeof(char)*(n+1));
      printf("Enter the string\n");
      scanf("%s",str);
      len = strlen(str);
      str[len]='\0';
      while(i<len)
      {
                  if(str[i+1] == str[i]+1)
                  {
                            printf("%c",str[i]);
                            i++;
                  }
                  else
                  {
                            if(str[i+1] == '\0')
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                            else
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                  }
      }
      getch();

}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void semicolonSeparatedSequenes(String input){
		int i = 0,first;
		char next;
		System.out.print(input.charAt(i));
		while( i < input.length()-1){
			//first = Integer.parseInt(Character.toString(input.charAt(i)));
			first = (input.charAt(i));
			//next = Integer.parseInt(Character.toString(input.charAt(i+1)));
			next = (input.charAt(i+1));
			if(next == first+1)
			System.out.print(next);
			else
				System.out.print(";" + next);
			i++;
		}
		
	}

- katz May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void semicolonSeparatedSequenes(String input){
		int i = 0,first;
		char next;
		System.out.print(input.charAt(i));
		while( i < input.length()-1){
			//first = Integer.parseInt(Character.toString(input.charAt(i)));
			first = (input.charAt(i));
			//next = Integer.parseInt(Character.toString(input.charAt(i+1)));
			next = (input.charAt(i+1));
			if(next == first+1)
			System.out.print(next);
			else
				System.out.print(";" + next);
			i++;
		}
		
	}

- katz May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;


public class SemiColonPettern {
public static void main(String arg[])
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the string");
String string=s.next();
StringBuffer buffer=new StringBuffer();
char c='a';
for (int i = 1; i < string.length(); i++) {
	if(i==1)
	buffer.append(string.charAt(0));
	if(buffer.charAt(buffer.length()-1)==';')
			{
		    buffer.append(string.charAt(i-1));
			}
	if(string.charAt(i-1)==string.charAt(i)-1)
	{
		buffer.append(string.charAt(i));
	}
	else
	{
		buffer.append(";");
	}
}
System.out.println(buffer);
}
}

- Anonymous November 07, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class PrintAlphabetsequence 
{

	static void print(String s)
	{
		int start =0 ,end = 0;
		char cur;
		s = s.toLowerCase();
		char prev =s.charAt(0);
		for(int i=1; i< s.length();i++)
		{
			cur =  s.charAt(i) ;
			if(cur == prev + 1)
			{
				end = i ;
				prev = cur;
				
			}
			else
			{   if( start != end)
				System.out.print(s.substring(start, end)+";");
				start = end = i;
				prev = cur;
			}	
			
		}
		if(start != end)
		{
			System.out.print(s.substring(start, end)+";");
		}
	}

	public static void main(String[] args)
	{
		print("AbcDefljdflsjflmnopflsjflasjftuvWxYz");
	}
	
}

- Jayanti March 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.Scanner;


public class charSeqs {
	public static void main(String[] args) {
		String in = "";
		StringBuffer sb=new StringBuffer();
		@SuppressWarnings("resource")
		Scanner s=new Scanner(System.in);
		System.out.println("input your string");
		    while(!in.equals("exit")) {
		        if(s.hasNext()) {
		            sb.append(in); in = s.next();
		            System.out.println(in);
		        }

		    } 
		    
		    System.out.println(sb);
		String strsb=sb.toString();
		strsb=strsb.toLowerCase();
		System.out.println(getSequences(strsb));

	}

	private static StringBuffer getSequences(String str) {
		
		char[] cha=str.toCharArray();
		StringBuffer sb=new StringBuffer();
		boolean[] flg=new boolean[cha.length];
		for(int i=0;i<cha.length-1;i++){
			if(cha[i]+1==cha[i+1]){
				flg[i+1]=true;
				sb.append(cha[i]);
			}else{
				if(flg[i]==true){
					sb.append(cha[i]);
					sb.append(";");
				}
			}
		}	
		if(flg[cha.length-1]==true){
			sb.append(cha[cha.length-1]);
		}
		return sb;
	}

}

- disun March 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Python working code.
Output of the OP's sample missing a 'l', should be:
abcdef; lmnop; tuvwxyz

"""
12:10
@Python 2.7

Print continuous alphabets from a sequence of arbitrary alphabets 
For example: 
Input: abcdefljdflsjflmnopflsjflasjftuvwxyz 
Output: abcdef; mnop; tuvwxyz 

Input: AbcDefljdflsjflmnopflsjflasjftuvWxYz 
Output: abcdef; mnop; tuvwxyz

- Anony on February 02, 2012 in United States Report Duplicate | Flag 
"""

class CountiA(object):
  def __init__(self, inputs):
    if inputs is None:
      print 'invalid inputs'
      raise SystemExit
    
    self._inputs = inputs
    
  def findSeq(self):
    output = []
    tmp = [self._inputs[0].lower()]
    lastChar = self._inputs[0].lower()
    for i in range(1, len(self._inputs)):
      if ord(self._inputs[i].lower()) - ord(lastChar) == 1:
        lastChar = self._inputs[i].lower()
        tmp.append(self._inputs[i].lower())
      else:
        if len(tmp) > 1:
          output.append(''.join(tmp))
        tmp = [self._inputs[i].lower()]
        lastChar = self._inputs[i].lower()
        
    if len(tmp) > 1:
      output.append(''.join(tmp))
      
    return '; '.join(output)
    
if __name__ == '__main__':
  ca = CountiA('abcdefljdflsjflmnopflsjflasjftuvwxyz')
  print ca.findSeq()
  ca = CountiA('AbcDefljdflsjflmnopflsjflasjftuvWxYz')
  print ca.findSeq()

- tosay.net March 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I think they have missed 'l' from the sequence "mnop" in the output. Here is a workin gcode in C++ considering the upper bound of the number of characters to be 100. we can make it dynamic is we wish.

#include <cstdio>
#include <iostream>
#include <string>

using namespace std;


void pp(char arr[], int st, int ed);
void pp(char arr[], int st, int ed)
{
    int i;
    for (i=st; i<ed; i++)
    {
        cout<<arr[i];
    }
}


int main()
{
    char arr[100];
    int i, st, ct=0;
    cin>>arr;
    for (i=0; i<strlen(arr); i++)
    {
        arr[i]=tolower(arr[i]);
    }
    st=0;
    i=1;
    ct=1;
    while (i<strlen(arr))
    {
        if (arr[i]!=arr[i-1]+1)
        {
            if (ct!=1)
            {
                pp(arr, st, i);
                cout<<"; ";
            }
            st=i;
            ct=1;
        }
        else
        {
            ct++;
        }
        i++;
    }
    pp(arr, st, i);
    return 0;
}

- Meraj Ahmed November 09, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static List<String> epic(String s) {
        List<String> r = new ArrayList<String>();
        int left = 0, right = 0;
        while (right < s.length()) {
            while (right < s.length() - 1 && s.charAt(right) + 1 == s.charAt(right + 1))
                right++;
            if (right - left >= 1) {
                r.add(s.substring(left,right+1));
            }
            left = right + 1;
            right = left;
        }
        return r;
    }

- chenyang.feng.cn April 07, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import java.util.*;
class charseq{
public static void main(String[] args){
String input ="AbcDefljdflsjflmnopflsjflasjftuvWxYz";
input = input.toLowerCase();
ArrayList arr = new ArrayList();
for(int i=0;i<input.length();i++){
arr.add(input.charAt(i));
}
Collections.sort(arr);
char prev = (char)arr.get(0);
char newchar ='-';
String str =""+prev;
String prevstr ="";
for(int i=1;i<arr.size();i++){
newchar=(char)arr.get(i);
if(prev == newchar) continue;
if(prev+1 == newchar){
str += newchar;
}
else{
if(prevstr != str){
str = str+";";
prevstr = str;
}
}
prev = newchar;
}
System.out.println(str);
}
}

- Vishnu Prasad May 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

please let me know if anything wrong with it im getting correct op

import java.util.*;
class charseq{
public static void main(String[] args){
String input ="AbcDefljdflsjflmnopflsjflasjftuvWxYz";
input = input.toLowerCase();
ArrayList arr = new ArrayList();
for(int i=0;i<input.length();i++){
arr.add(input.charAt(i));
}
Collections.sort(arr);
char prev = (char)arr.get(0);
char newchar ='-';
String str =""+prev;
String prevstr ="";
for(int i=1;i<arr.size();i++){
newchar=(char)arr.get(i);
if(prev == newchar) continue;
if(prev+1 == newchar){
str += newchar;
}
else{
if(prevstr != str){
str = str+";";
prevstr = str;
}
}
prev = newchar;
}
System.out.println(str);
}
}

- Vishnu Prasad May 24, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class continousAlphabets{

public static void main(String args[])
{
String str = "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
str = str.toLowerCase();
int i = 0;
String temp = "";
while(i<str.length()-1){

if(str.charAt(i+1) == str.charAt(i) + 1)
{
temp = temp + str.charAt(i);
i++;
}
else
{
temp = temp + str.charAt(i);
if(temp.length() >=2){
System.out.println(temp + ";");
}
temp = "";
i++;
continue;
}
}
if(temp.charAt(temp.length() -1) + 1 == str.charAt(str.length()-1)){
temp = temp + str.charAt(str.length()-1);
}
System.out.println(temp);
}

}

- Anonymous October 18, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

If we add an invalid char by the end of the string, we don't need to worry about the last char. Given we are studying alphabet, so I add a space to the end, and the space won't continuous with any char.

package EPIC;

public class ContinuousAlphabets {
	public static void printContiAlpha(String in){
		String S = in.toLowerCase();
		S = S + ' ';
		char curr;
		char next;
		boolean continuous = false;
		
		for (int i = 0; i <= S.length() - 2; i++) {
			curr = S.charAt(i);
			next = S.charAt(i + 1);
			
			if(curr + 1 == next){
				System.out.print(curr);
				continuous = true;
			}else if( continuous ){
				System.out.print(curr + "; ");
				continuous = false;
			}
		}
	}
	
	public static void main(String[] args) {
		String in = "AbcDefljdflsjflmnopflsjflasjftuvWxYz";
		printContiAlpha(in);
	}
}

- albertchenyu March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
#include<conio.h>
main()
{
      int i=0, len, n;
      printf("Enter the length of the string\n");
      scanf("%d", &n);
      char *str = (char *)malloc(sizeof(char)*(n+1));
      printf("Enter the string\n");
      scanf("%s",str);
      len = strlen(str);
      str[len]='\0';
      while(i<len)
      {
                  if(str[i+1] == str[i]+1)
                  {
                            printf("%c",str[i]);
                            i++;
                  }
                  else
                  {
                            if(str[i+1] == '\0')
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                            else
                            {
                                        printf("%c;",str[i]);
                                        i++;
                            }
                  }
      }
      getch();
}

- vibhor gupta March 28, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

The working code for this can be found in this website (nobrainer.co.cc)

- Anony February 02, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 2 vote

public void semicolonSeparatedSequenes(String input){
		int i = 0,first;
		char next;
		System.out.print(input.charAt(i));
		while( i < input.length()-1){
			first = (input.charAt(i));
			next = (input.charAt(i+1));
			if(next == first+1)
			System.out.print(next);
			else
				System.out.print(";" + next);
			i++;
		}
		
	}

- katz May 05, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

For the input at the above i.e "AbcDefljdflsjflmnopflsjflasjftuvWxYz"
Your code produces this output
A;bc;D;ef;l;j;d;f;l;s;j;f;lmnop;f;l;s;j;f;l;a;s;j;f;tuv;W;x;Y;z
but it should be
abcdef; lmnop; tuvwxyz

- thepraveen0207 October 25, 2012 | Flag


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