Microsoft Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
/*
ideone.com/4371r
*/
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
String a = "{A:'B',C:{D:'E',F:{G:'H',I:'J'
";
int level = 0;
String p;
for(int i=0; i<a.length();i++)
{
char c = a.charAt(i);
String s = Character.toString(c);
if( s.equals("{"))
{
level++;
int count = 0;
System.out.print(s+"\n");
while(count<level)
{
System.out.print(" ");
count++;
}
}
else if(s.equals(","))
{
int count = 0;
System.out.print(s+"\n");
while(count<level)
{
System.out.print(" ");
count++;
}
}
else if( s.equals("}"))
{level--;
int count = 0;
System.out.print("\n");
while(count<level)
{
System.out.print(" ");
count++;
}
System.out.print(s);
}
else
{
System.out.print(s);
}
}
}
}
public static void printJSON(String input, StringBuffer spaces, boolean useSpaces)
{
char printChar= input.charAt(0);
System.out.print(useSpaces?spaces.toString()+printChar:printChar);
if(input.length()<=1)
return;
useSpaces= true;
switch(printChar)
{
case ',':
System.out.print("\b\n");
break;
case ':' :
useSpaces= input.charAt(1)=='{';
if(useSpaces)
System.out.println();
break;
case '{':
System.out.println();
spaces= spaces.append(" ");
break;
case '}':
System.out.println();
spaces= spaces.length()>0?spaces.deleteCharAt(0):spaces;
break;
default:
useSpaces= input.charAt(1)=='}';
if(useSpaces)
System.out.println();
}
printJSON(input.substring(1, input.length()), spaces, useSpaces);
}
string t="";
void indent(int indent_counter)
{
while(indent_counter--)
t+="\t";
}
int main()
{
string s;
cin >> s;
int len=s.length();
int indent_counter=0;
s+='p';
for(int i=0;i<len;i++)
{
if(s[i]=='{')
{
indent(indent_counter);
if(s[i+1]!='{' || s[i+1]!='}')
{
t+="\n";
indent(indent_counter);
t+="{\n";
}
else
{
t+="\n";
indent(indent_counter);
t+="{";
}
indent(++indent_counter);
}
else if(s[i]=='}')
{
if(s[i+1]!='}' || s[i+1]!='{')
{
t+="\n";
indent(--indent_counter);
t+="}\n";
}
else
{
t+="\n";
indent(--indent_counter);
t+="}";
}
indent(indent_counter);
}
else if(s[i]==',')
{
t+="\n";
indent(indent_counter);
}
else
{
t+=s[i];
//indent(indent_counter);
}
//cout <<i<<"\t"<< s[i]<<endl;
}
cout << t<<endl;
return 0;
}
public static void indent(char[] A)
{
int tab=0;
int count=0;
for(int i=0;i<A.length;i++)
{
if(A[i]=='{')
{
count=0;
System.out.print("\n");
for(int j=0;j<tab;j++)
System.out.print("\t");
System.out.print("{");
System.out.print("\n");
tab++;
}
else if(A[i]==',')
{
count=0;
System.out.print("\n");
}
else if(A[i]=='}')
{
count=0;
tab--;
System.out.print("\n");
for(int j=0;j<tab;j++)
System.out.print("\t");
System.out.print("}");
System.out.print("\n");
if(tab<0)
System.out.print("error");
}
else
{
if(count==0)
{
for(int j=0;j<tab;j++)
System.out.print("\t");
}
System.out.print(A[i]);
count=1;
}
public static void main(String[] pp)
{
char[] A ="{A:\"B\",C:{D:\"E\",F:{G:\"H\",I:\"J\"
".toCharArray() ;
indent(A);
}
}
}
Code :
#include <stdio.h>
#define MAX_LENGTH 100
int is_special(char ch)
{
if(ch == ',' || ch == '{' || ch == '}')
return 1;
return 0;
}
main()
{
char S[MAX_LENGTH];
int i, alph = 0;
scanf("%s", S);
for(i = 0; S[i] != '\0'; i++)
{
if(is_special(S[i]))
{
if(alph)
printf("\n");
if(S[i] != ',')
printf("%c\n", S[i]);
alph = 0;
}
else
{
printf("%c", S[i]);
alph = 1;
}
}
return 0;
}
Please give me your suggestions.
My indentation did not appear in the question.
- Naveen Reddy Mandadi August 25, 2012Every inner brace should increase one indentation to the following lines.
Every close brace should decrease one indentation to the same line and the following lines.