Microsoft Interview Question
Applications DevelopersCountry: India
Interview Type: In-Person
#include <iostream>
void removeDoubleZeros(char* src)
{
int len = strlen(src);
int idxSrc = 0;
int idxDest = 0;
while (idxSrc + 1 < len)
{
if (src[idxSrc] == '0' && src[idxSrc + 1] == '0')
{
if (
((idxSrc - 1 >= 0 && src[idxSrc - 1] != '0') || idxSrc - 1 < 0) &&
((idxSrc + 2 < len && src[idxSrc + 2] != '0') || idxSrc + 2 >= len)
)
{
idxSrc += 2;
}
else
src[idxDest++] = src[idxSrc++];
}
else
{
src[idxDest++] = src[idxSrc++];
}
}
src[idxDest++] = src[idxSrc];
src[idxDest] = 0;
};
int main()
{
char src[] = "a3409jd00dk000d";
removeDoubleZeros(src);
return 0;
}
This is inplace and with one Iteration.
void remove2zeros(char *input)
{
char *temp;
temp = input;
while (*temp != '\0')
{
if (*temp != '0')
{
if (input != temp)
{
*input = *temp;
input++;temp++;
}
else
{
temp++;input++;
}
}
else
{
if (*(temp-1) != '0' && *(temp+1) == '0' && *(temp+2) != '0')
{
if (input == temp)
{
temp = temp + 2;
}
else
{
temp = temp + 2;
}
}
else
{
if (input == temp)
{
input++;temp++;
}
else
{
*input = *temp;input++;temp++;
}
}
}
}
*input = '\0';
}
/*
Remove all two zeros in the given string.
Example: a3409jd00dk000d
Output: a3409jddk000d
Note: If there are less/more than two zeros consecutive, it should not be replaced.
ideone.com/tYsRf
*/
import java.util.*;
import java.lang.*;
class Main
{
public static boolean check(int i, char[] s)
{
if ((i < s.length) && (s[i] == '0')) return true;
else return false;
}
public static void main (String[] args) throws java.lang.Exception
{
String a = "a3409jd00dk0000000d00e";
char[] s = a.toCharArray();
for(int i=0; i<s.length; i++)
{
if(s[i]== '0' && check(i+1, s))
{
if (!check(i+2, s))
System.out.println("Position of 2 consecutive 0's - i: "+i);
else
i = i+2;
}
}
}
}
Here is a recursive solution, obviously iterative should be preferred.
public static String eraseCouples(StringBuffer input, boolean zeroStreak)
{
if(input.length()>=2&&input.charAt(0)=='0'&&input.charAt(1)=='0')
if(input.charAt(2)!='0'&&!zeroStreak)
return eraseCouples(input.delete(0, 2), false);
else
return input.charAt(0)+ eraseCouples(input.deleteCharAt(0), true);
else if(input.length()>0)
return input.charAt(0)+ eraseCouples(input.deleteCharAt(0), false);
else return "";
}
#include<stdio.h>
#include<string.h>
int main()
{
char str1[20]="a3409jd00dk0009";
char str2[20];
int len=strlen(str1);
for(int i=0,j=0;i<len;i++,j++)
{
if(str1[i]=='0' && str1[i+1]=='0')
if(str1[i+2]!='0')
i+=2;
else
for(;str1[i]=='0';str2[j++]=str1[i++]) ;
str2[j]=str1[i];
}
str2[j]='\0';
printf("\n%s",str2);
return 0;
}
#include<iostream>
using namespace std;
int main(){
string s = "a3409jd00dk000d";
cout<<s<<endl;
int state = 0;
// cout<<s[5];
for(int i(0);i< s.length() ; i++)
{
if(state ==0 &&s[i] != '0') {state = 0; cout<<s[i];}
else if (state =0 && s[i] =='0' ) state = 1;
else if (state =1 && s[i] !='0' ) {cout<<"0"<<s[i]; state =0;}
else if (state =1 && s[i] =='0' ) state = 2;
else if (state =2 && s[i] !='0' ) { state =0; cout<<s[i];}
else if (state =2 && s[i] =='0' ) {cout<<"00"; state = 3;}
else if (state =3 && s[i] !='0' ) { state =0; cout<<"0"<<s[i];}
else if (state =3 && s[i] =='0' ) {cout<<s[i];}
}
return 1;
}
I think there is absolutely no need of altering the original array because after all we need is an array that prints out everything except every 2 consequtive zeroes. so perhaps wherever we encounter those while scanning simply replace with '\007' or bell character and rest will remain as it is...
public static string remove00(string str)
{
int index = 0, len = str.Length;
string retstr = "";
string temp = "";
int ctr = 0;
while (index < len)
{
if (str[index] == '0')
{
temp += '0';
ctr++;
if (index == len - 1 && ctr != 2)
{
retstr += temp;
}
}
else
{
if (ctr != 2)
{
retstr += temp;
ctr = 0;
}
retstr += str[index];
}
index++;
}
return(retstr);
}
public static string remove00(string str)
{
int index = 0, len = str.Length;
string retstr = "";
string temp = "";
int ctr = 0;
while (index < len)
{
if (str[index] == '0')
{
temp += '0';
ctr++;
if (index == len - 1 && ctr != 2)
{
retstr += temp;
}
}
else
{
if (ctr != 2)
{
retstr += temp;
ctr = 0;
}
retstr += str[index];
}
index++;
}
return(retstr);
}
public static string remove00(string str)
{
int index = 0, len = str.Length;
string retstr = "";
string temp = "";
int ctr = 0;
while (index < len)
{
if (str[index] == '0')
{
temp += '0';
ctr++;
if (index == len - 1 && ctr != 2)
{
retstr += temp;
temp = "";
}
}
else
{
if (ctr != 2)
{
retstr += temp;
}
temp = "";
ctr = 0;
retstr += str[index];
}
index++;
}
return(retstr);
}
public static string remove00(string str)
{
int index = 0, len = str.Length;
string retstr = "";
string temp = "";
int ctr = 0;
while (index < len)
{
if (str[index] == '0')
{
temp += '0';
ctr++;
if (index == len - 1 && ctr != 2)
{
retstr += temp;
temp = "";
}
}
else
{
if (ctr != 2)
{
retstr += temp;
}
temp = "";
ctr = 0;
retstr += str[index];
}
index++;
}
return(retstr);
}
judge my code plzz thanxx in advance
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,k,temp=0,temp1;
char str[1000000];
scanf("%s",str);
k=strlen(str);
for(i=0;i<k;i++)
{
temp=0;
if(str[i]=='0')
{
temp=temp+1;
j=i+1;
while(str[j]=='0')
{
j=j+1;
temp=temp+1;
}
if(temp!=2)
{
temp1=1;
while(temp1<=temp)
{
printf("0");
temp1=temp1+1;
}
i=i+temp-1;
}
else{
i=i+1;
}
}
else{
printf("%c",str[i]);
}
}
return 0;
}
import java.util.*;
public class sortarray {
public static int replaceZeros(char[] A)
{
int k=A.length;
for(int i=0;i<k-2;i++)
{
System.out.println("hello"+k);
if((i+3==k && A[i]!='0')||(A[i]!='0' && A[i+3]!='0') && (A[i+1]=='0' && A[i+2]=='0'))
{
for(int j=i+1;j<k-2;j++)
{
A[j]=A[j+2];
}
k=k-2;
}
}
return k;
}
public static void main(String[] pp)
{
//Scanner charctr=new Scanner(System.in);
char[] A =new char[15] ;
for(int i=0;i<15;i++)
{
A[i]='0';
}
//a3409jd00dk0009
A[0]='a';A[2]='4';
A[1]='3';A[4]='9';A[5]='j';A[6]='d';A[9]='d';A[10]='k';A[14]='9';
int k=replaceZeros(A);
for(int i=0;i<k;i++)
{
System.out.println(A[i]);
}
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
char a[20];
int i,j,ctr=0,x=0;
printf("Enter the string : ");
scanf("%[^\n]d",&a);
for(i=0;i<strlen(a);i++)
{
if(a[i] == '0')
{
ctr++;
x=i;
}
if(ctr==2 && i == x+1)
{
for(j=i;j<strlen(a);j++)
a[j-2]=a[j];
a[j-2]='\0';
ctr=0;
i=i+1;
}
else if(ctr!=2 && a[i+1] != '0')
ctr=0;
}
printf("\nOutput String : %s",a);
getch();
return 0;
}
This is a C function that the parameter is called by reference. The logic is so simple, start copying the characters from pointer to the base pointer. When two 0 have seen by prt and then a none 0 appear decrement base pointer by 2. At the end close the string.
because this is a call by reference function the string in caller method will change. Easily you cn convert to a call by value function and return the result as a return value.
void remove2zeros(char *str)
{
int flag = 0;
char *p,*base;
p=base=str;
while(*p!='\0')
{
if(*p=='0')
flag++;
else {
if(flag==2)
base-=2;
flag=0;
}
*base=*p;
base++;
p++;
}
*base='\0';
return;
}
#include<stdio.h>
void remove2zero(char arr[],int s)
{
int i;
for(i=0;i<s;i++)
{
if(arr[i-1]!='0' && arr[i]=='0' && arr[i+1]=='0' && arr[i+2]!='0')
i=i+1;
else
printf("%c",arr[i]);
}
printf("\n");
}
int main()
{
int s;
char arr[]="a34009jd00d0000k000d";
s=strlen(arr);
remove2zero(arr,s);
return 0;
}
public static void main(String[] args) {
String string = "a340weq00q000000e0002300er";
string= string.replace("00", "/");
string=string.replace("//", "0000");
string=string.replace("/0", "000");
string=string.replace("0/", "000");
string=string.replace("/", "");
System.out.println(string);
}
How is this code :
#include <stdio.h>
main()
{
char S[] = "00x0000ac00ac00a00";
int i, j = -1, count = 0;
for(i = 0; S[i] != '\0'; i++)
{
if(S[i] == '0')
{
count++;
if(count >= 3)
{
if(count == 3)
{
S[++j] = '0';
S[++j] = '0';
}
S[++j] = '0';
}
}
else
{
if(count == 1)
S[++j] = '0';
S[++j] = S[i];
count = 0;
}
}
S[++j] = '\0';
printf("%s\n", S);
return 0;
}
Please let me know if you find it difficult to understand before reducing -ve voting.
public static String remove2Zeroes (final String input) {
//Example: a3409jd00dk000d
//Output: a3409jddk000d
int i =0;
int len =input.length();
char c = input.charAt(0);
int cnt = 0;
StringBuffer buf = new StringBuffer();
while( i < len) {
c = input.charAt(i);
if( c != '0') {
if( cnt != 2) {
for( int j = 0; j < cnt; ++j) {
buf.append('0');
}
}
buf.append(c);
cnt = 0;
}
else {
cnt++;
}
++i;
}
//Ending zeroes
if( cnt != 2) {
for( int j = 0; j < cnt; ++j) {
buf.append('0');
}
}
return buf.toString();
}
#include<iostream.h>
int main (int argc, char **argv)
{
char *str=argv[1];
char *p=str;
int zerocount =0;
while(*p!='\0')
{
if(*p == '0')
{
zerocount++;
if(zerocount >2)
{
cout << *p;
}
}
else
{
if(zerocount!=0 && zerocount!=2)
{
cout << '0';
if(zerocount!=1)
cout << '0';
}
cout <<*p;
zerocount=0;
}
p++;
}
}
#include<stdio.h>
void remove2zero(char *str){
char *tmp=str;int flag=0;
int i;
while(*tmp){
if(*tmp!='0'){
if(flag==2 || flag==0){
*str++=*tmp;
if(flag==2) flag=0;
}
else {
for(i=flag;i>=0;i--)
*str++=*(tmp-i);
flag=0;
}
}
else
flag++;
tmp++;
}
if(flag!=0 || flag!=2){
for(i=flag;i>=0;i--)
*str++=*(tmp-i);
}
*str='\0';
}
main(){
char str[20];
printf("enter string \n");
scanf("%s",str);
remove2zero(str);
printf("%s",str);
}
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char ch[50];
int i,j;
gets(ch);
i=0;
while(ch[i]!='\0')
{
if(ch[i]=='0'&&ch[i+1]=='0')
{
if(ch[i+2]=='0')
{
j=i+2;
while(ch[j]!='\0'&&ch[j]=='0')
{
j++;
}
i=j;
}
else
{
j=i;
while(ch[j-1]!='\0')
{
ch[j]=ch[j+2];
j++;
}
i=i+1;
}
}
else
i++;
}
puts(ch);
_getch();
}
char testString[]="100200300400500060700003";
int index[50];
int i=0;
int len2;
int j=0;
len2=strlen(testString);
for(i=0;i<len2;i++)
{
if((testString[i]=='0')&&(testString[i+1]=='0'))
{
if(testString[i+2]!='0')
{
index[j]=i;
j++;
}
else i=i+2;
}
}
for(i=j-1;i>=0;i--)
{
sprintf(&testString[index[i]],&testString[index[i]+2]);
}
len=2*j;
testString[len2-2*j]='\0';
int EliminateZero(char *pStr, int len)
{
if(!pStr || len == 0)
return -1;
int CaughtCount = 0;
for(int index = 0; index < len; ++index)
{
if(pStr[index] != '0')
{
if(CaughtCount == 1)
CaughtCount = 0;
else if(CaughtCount == 2)
{
CaughtCount = 0;
memcpy(pStr + index - 2,pStr + index,len - index);
index -= 2;
len -= 2;
}
else if(CaughtCount > 2)
CaughtCount = 0;
continue;
}
else
{
++CaughtCount;
}
}
pStr[len] = '\0';
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
char p[] = "34900543205000";
int size = strlen(p);
EliminateZero(p,size);
return 0;
}
static void ReplaceDoubleZeros(string s)
{
if (s.Length <= 1) return;
int temp = 0;
StringBuilder sb = new StringBuilder(s.Length);
for (int i = 0; i < s.Length - 1; i++)
{
if (s[i] == '0')
{
temp = i;
while (s[++temp] == '0')
;
if (temp - i == 2)
i = temp;
}
//Append the number of if # of zeros != 2
while (--temp - i > 0)
sb.Append(s[i]);
sb.Append(s[i]);
}
Console.WriteLine(sb);
}
static void ReplaceDoubleZeros(string s)
{
if (s.Length <= 1) return;
int temp = 0;
StringBuilder sb = new StringBuilder(s.Length);
for (int i = 0; i < s.Length - 1; i++)
{
if (s[i] == '0')
{
temp = i;
while (s[++temp] == '0')
;
if (temp - i == 2)
i = temp;
}
//Append the number of if # of zeros != 2
while (--temp - i > 0)
sb.Append(s[i]);
sb.Append(s[i]);
}
Console.WriteLine(sb);
}
public String remove00(String str)
{
int index=0,length=str.length();
char ch;
StringBuffer sbuf=new StringBuffer(str);
while(index<length)
{
ch=sbuf.charAt(index);
if(ch=='0')
{
if(sbuf.charAt(index-1)!='0' && sbuf.charAt(index+1)=='0' && (index+2==length || sbuf.charAt(index+2)!='0') )
{
sbuf.setCharAt(index,ch);
sbuf.setCharAt(index+1,'\b');
index+=2;
continue;
}
}
index++;
}
return sbuf.toString();
}
This code will throw Exceptions at almost every conceivable boundary value.
Try 00a000k00 as an example.
#include "stdio.h"
#include "conio.h"
int main(){
char str[30]="00x0000ac00ac00a00";
int i;
int j;
for(i=0;i<strlen(str)-1;i++){
if(str[i]=='0'){
if(str[i+1]=='0'){
if(str[i+2]=='0'){
i=i+2;
while(!str[i]){
i++;
}
}
else{
for(j=i;j<=strlen(str);j++){
str[j]=str[j+2];
}
}
}
}
}
printf("%s",str);
getch();
}
- pranaymahajan August 27, 2012