Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
1
of 3 vote

#include <stdio.h>

char* removeSingleOccurrence(char* s, char c)
{
    int i = 0, pos = 0, cnt;
    while(s[i]){
        //if not c then append s[i] to s[pos]
        if(s[i] != c) s[pos++] = s[i++];
        else{
            //count how many times c occurs repeatedly
            cnt = 1;
            for(++i; s[i] && s[i] == c; ++i, ++cnt) ;
            //if times > 1 append c to s[pos] cnt times
            if(cnt > 1){
                for(; cnt; --cnt) s[pos++] = c;
            }
        }
    }
    s[pos] = '\0';
    return s;
}

int main()
{
    char s[] = "120jdvj00ncdnv000ndnv0nvd0nvd0";
    puts(removeSingleOccurrence(s, '0'));
    return 0;
}

- uuuouou May 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Another Solution In C

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int n,i,count=1,j=0;
    char str[50];
    char ch='0';
    gets(str);
	n=strlen(str);
	char str2[30];

	for(i=0;i<n;i++){

			if(str[i]==ch)
   				{
   				     count++;

   				    if(count==1)
   				       printf("");
                    else if (count==2)
                    {
                         str2[j++]='0';
                         str2[j++]=str[i];
                    }
                    else
                         str2[j++]=str[i];

   				}
			else
                {
                    count=0;
                    str2[j++]=str[i];
     			}
	}
	str2[j]='\0';
	printf("%s",str2);
return 0;
}

- Rohit Jain November 04, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>

char* answer(char s[], char ch){
int i;
int k = 0;
for(i = 0; s[i] != '\0'; i++){
// printf("i = %d", i);
if(s[i] == ch){
if(s[i+1] != '\0' && s[i+1] == ch){
int counter = 0;
while(s[i] != '\0' && s[i] == ch){
counter++;
s[k] = s[i];
i++;
k++;
}
i--;
}
else {
// Nothing
}
}
else {
*(s+k) = *(s+i);
k++;
}
}
s[k] = '\0';
return s;
}

int main(){
char s[] = "120jdvj00ncdnv000ndnv0nvd0nvd0";
char ch = '0';
printf("%s", answer(s, ch));
}

- shubh_211 June 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	char str[20]="abacdfadaahiaaaaba";
	int i=0,j=0;
	int size=strlen(str);
	while(i<size){
		if(str[i]!='a'|| (str[i]=='a' && (str[i+1]=='a' || str[i-1]=='a'))){
			str[j]=str[i];
			j++;
		}
		i++;
	}

for(int i=0;i<j;i++) cout<<str[i];

return 0;
}

- shweta aggarwal June 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cstring>
using namespace std;
int main(){
char str[20]="abacdfadaahiaaaaba";
int i=0,j=0;
int size=strlen(str);
while(i<size){
if(str[i]!='a'|| (str[i]=='a' && (str[i+1]=='a' || str[i-1]=='a'))){
str[j]=str[i];
j++;
}
i++;
}

for(int i=0;i<j;i++) cout<<str[i];

return 0;
}

- shweta aggarwal June 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<cstring>
using namespace std;
int main(){
	char str[20]="abacdfadaahiaaaaba";
	int i=0,j=0;
	int size=strlen(str);
	while(i<size){
		if(str[i]!='a'|| (str[i]=='a' && (str[i+1]=='a' || str[i-1]=='a'))){
			str[j]=str[i];
			j++;
		}
		i++;
	}

for(int i=0;i<j;i++) cout<<str[i];

return 0;
}

- shwetagupta.edu June 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Guys,

It's very similar to solutions above. However, I have 2 pointers, one that increments for each character or the input string and the other that increments with the output string. Let me know if you guys find something wrong with it.

#include <stdio.h>                                                                                                    
                                                                                                                      
int remove_if_not_simultaneous(char * s, char c) {                                                                    
  char * temp1 = s;                                                                                                   
  char * temp2 = s;                                                                                                   
  int consecutive = 0;                                                                                                
  while (*temp1 != '\0') {                                                                                            
    if (*temp1 == c) {                                                                                                
      while ((*(temp1+1) != '\0') && (*(temp1+1) == c)) {                                                             
        *temp2 = *temp1;                                                                                              
        temp1++;                                                                                                      
        temp2++;                                                                                                      
        consecutive = 1;                                                                                              
      }                                                                                                               
      if (!consecutive) {                                                                                             
        temp1++;                                                                                                      
      }                                                                                                               
    }                                                                                                                 
    consecutive = 0;                                                                                                  
    *temp2 = *temp1;                                                                                                  
    temp1++;                                                                                                          
    temp2++;                                                                                                          
  }                                                                                                                   
  *temp2 = '\0';                                                                                                      
  return 1;                                                                                                           
}                                                                                                                     
                                                                                                                      
int main () {                                                                                                         
  char s[] = "120jdvj00ncdnv000ndnv0nvd0nvd0";                                                                        
  printf("%s\n",s);                                                                                                   
  remove_if_not_simultaneous(s,'0');                                                                                  
  printf("%s\n",s);                                                                                                   
  return 0;                                                                                                           
}

- Anonymous June 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess I can't remove this without logging in here.

- hiya June 26, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Hi Guys,

I have a similar solution as above, but with 2 pointers, one that stays with the initial string during parse, and the second that stays at the end of the resulting string. Please have a look and let me know if you find something wrong.

#include <stdio.h>

int remove_if_not_simultaneous(char * s, char c) {
  char * temp1 = s;
  char * temp2 = s;
  int consecutive = 0;
  while (*temp1 != '\0') {
    if (*temp1 == c) {
      while ((*(temp1+1) != '\0') && (*(temp1+1) == c)) {
	*temp2 = *temp1;
	temp1++;
	temp2++;
	consecutive = 1;
      }
      if (!consecutive) {
	temp1++;
      }
    }
    consecutive = 0;
    *temp2 = *temp1;
    temp1++;
    temp2++;
  }
  *temp2 = '\0';
  return 1;
}

int main () {
  char s[] = "120jdvj00ncdnv000ndnv0nvd0nvd0";
  printf("%s\n",s);
  remove_if_not_simultaneous(s,'0');
  printf("%s\n",s);
  return 0;
}

- hiya June 26, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RepalceSingleOccurance(char* str, char ch)
{
  int i=0, j=0;
  while(str[i] != '\0')
  {
    if(str[i] != ch)
    {
		str[j] = str[i];
		j++; i++;
	}
	else if(str[i+1] == ch)
	{
	    while(str[i] == ch)
		{
			str[j] = str[i];
			j++; i++;
		}
	}
	else
	{
		i++;
	}
  }
  str[j] = '\0';
}

- CCoder July 06, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void removeFromStrCharNonSimultnaious ()
{
	char str[100];
	char ch;
	int idx = 0, rIdx;
	printf ("Enter String \n");fflush (stdout);
	scanf  ("%s",str);
	fflush (stdout);fflush (stdin);
	printf ("Enter Char \n");fflush (stdout);
	scanf ("%c",&ch);

	while (str[idx] != '\0')
	{
		if (str[idx] == ch)
		{
			if (str[idx - 1] != ch && str[idx +1] != ch)
			{
				//shift elements to left to remove ch
				rIdx = idx;

				while (str[rIdx] != '\0')
				{
					str[rIdx] = str[rIdx + 1];
					rIdx++;
				}
				continue;
			}
		}
		idx++;
	}

	printf ("Updated String %s\n",str);
}

- ravneetsingh1986 August 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Simple Implementation

{
char* rem_char(static char *s)
{

int len;
int i,j,k;
int final_addr=0;
char *final_str,*ret;

len=strlen(s);
final_str = (char*)malloc(len*sizeof(char));
*final_str = NULL;
ret = final_str;
while(*s!='\0')
{
if(*s=='o')
{
if(*(s+1)=='o')
{
goto INC;
}
}
*final_str++=*s;
INC:
s++;
}
*final_str='\0';
return ret;

}
int main()
{
char s[]="fadoodooood";
//printf("\n Enter the string containing repeated a chars");
//gets(s);
printf("\n The entered character is %s",s);
printf("Modified string = %s",rem_char(s));
getch();
return 0;
}

}

- Sobin Thomas September 05, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>

void removeNShift(char * str, int pos){
    if(pos != strlen(str) - 1){
        int i;
        for(i = pos;i <= strlen(str) - 2;i++)   str[i] = str[i + 1];
    }
    str[strlen(str) - 1] = '\0';
}

main()
{
    char str[] = "0120jdvj00ncdnv000ndnv0nvd0nvd0";
    if(str[0] == '0' && str[1] != '0')  removeNShift(str, 0);
    if(str[strlen(str) - 1] == '0' && str[strlen(str) - 2] != '0')  removeNShift(str, strlen(str) - 1);
    int j;
    for(j = 1;j <= strlen(str) - 2;j++){
        if(str[j] == '0' && str[j - 1] != '0' && str[j + 1] != '0')  removeNShift(str, j);
    }
    printf("%s\n", str);
}

- kshafiee September 25, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <iostream>
#include <cmath>
#include "stdlib.h"
#include "string.h"

using namespace std;

void strip(string s,int length )
{
	int i,j;
	for(i=0;i<length;i++)
	{
		if(s[i]=='0'&&s[i+1]=='0')
		i=i+2;
		else if(s[i]=='0'&& s[i+1]!='0')
		{
			for(j=i;j<length;j++)
			s[j]=s[j+1];
		}
	}
	cout<<"The modified string is:"<<s;
}


int main()
{
	int length;
	string s;
	cout<<"Enter the string\n";
	cin>>s;	
	length=s.size();
	cout<<"The length is"<<length;
//	cout<<s[0];
	strip(s,length);
	return 0;

- Mal September 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Sorry for the inconvenience. I thought we have to remove simultaneous occurring characters. But now i have changed it for removing single occurrence of a character

#include <stdio.h>
#include <conio.h>
#include <string.h>
void removeSingleChar(char *s,char c,int i,int pos,char prev)
{
	if(s[i]=='\0')
	{
		s[pos]='\0';
		for(int j=0;j<pos;j++)
			printf("%c",s[j]);
		return;
	}
	else if(prev==s[i])
	{
		s[pos++]=s[i];
	}		
	else if((s[i]!=s[i+1]&&s[i]!=c)||(s[i]==s[i+1]))
	{
		s[pos++]=s[i];
		prev=s[i];
	}
	i++;
	removeSingleChar(s,c,i,pos,prev);		
}
int main()
{
	char str[]="a000b000";
	char prev='\0';
	removeSingleChar(str,'0',0,0,prev);
	
}

- vgeek May 27, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Have you ever tested your code? You didn't alloc space for char* str. And your algorithm wouldn't work with string such as "a" whose last character is not '0'.

- ravio May 27, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Ravio, although I have changed the code now but for your query of char *str. No we do need to allocate space for this. Compiler does automatically when the program is compiled. Because if you will write char str[20] then this is also equivalent to
char *(str+20). In char *str i just allocates the base address of an array and then from that base address you can further expand the array by indexing it

- vgeek May 28, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

public class RemoveZero {
public static void main(String[] args) {
	

	remove("120jdvj00ncdnv000ndnv0nvd0nvd0" );
}


static void remove(String str)
{
int k=0;
	for(int i=0;i<str.length()-1;i++)
	{
		k++;
		if(str.charAt(i)=='0' && str.charAt(i+1)=='0' )
		{
			i++;
			k++;
			if(str.charAt(i)=='0' && str.charAt(i+1)=='0' )
			{
				i++;
				k++;
			}
		}
		else
			System.out.print(str.charAt(i));
	}
	//System.out.print(str.charAt(k));
	System.out.println();
	System.out.println(str);
	
}
}

- Ravi Kumar May 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

main()
{
    char s[] = "120jdvj00ncdnv000ndnv0nvd0nvd0";
    char  *t;
    t = (char *) malloc(0);
    int idx = 0;
    int t_idx = 0;
    char comp = '0';
    while(idx < strlen(s)) {
        if(s[idx] == '0' && (s[idx + 1] == comp || s[idx - 1] == comp)) {
            t = (char *) realloc(t, strlen(t) + 1);
            t[t_idx] = s[idx];
            t_idx++;
        } else if(s[idx] != '0') {
            t = (char *) realloc(t, strlen(t) + 1);
            t[t_idx] = s[idx];
            t_idx++;
        }
        idx++;
    }
    printf(t);
}

- Kagutsuchi May 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<iostream>

using namespace std;

void RemoveSingleZero(char *p)
{
    int s = strlen(p);

    int j = 0;
    int i = 0;
    for(i = 0; i < s; )//do  not increment i here
    {
         if(p[j] == '0')
         {
             bool flag = false;

             if(j >= 1)
             {
               flag =  (p[j-1] == '0');
             }

             if(j < s-1)
             {
               flag = flag || ( p[j+1] == '0');
                
             }
            
             if(flag)
             {
                p[i] = p[j];
                i++;
                j++;
             }
             else
             {
                 j++;
             }
         }
         else
         {
            p[i++] = p[j++];
         }
    }

    p[i] = '\0';

}

int main()
{
  char arr[] = "120jdvj00ncdnv000ndnv0nvd0nvd0";
  RemoveSingleZero(arr);
  cout<<arr;

}

- Satveer Singh May 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
#include<conio.h>
void collect(char *,char);
void main()
{
char s[]="120jdvj00ncdnv000ndnv0nvd0nvd0";
clrscr();
collect(s,'0');
getch();
}
void collect(char *s,char c){
int i=0,j=0,k,count,m;
while(s[i]) {
if(s[i]!=c) {
s[j]=s[i];
j++;
i++; }
else {
k=i;
count=0;
while(s[k]==c) {
count++;
k++ ; }
if(count==1)
i=k;
if(count>1) {
while(count!=0){
s[j]=s[i];
j++;
i++;
count--;} }
k=0;
} }
s[j]='\0';
k=0;
while(s[k]!='\0') {
printf("%c",s[k]);
k++;
}}

- keshav May 28, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void rmoveSingleOccurence(char * str, char c){
    int len = strlen(str);
    
    // mark all single occurrences as \0
    for(int i =0; i<len;i++){
        if(str[i]==c){
            //check for next char
            if(str[i+1]==c) { while(str[i+1]==c) i++;}
            else str[i]='\0';
        }
    }
    
    // compress the array
    int j=0;
    for(int i=0; i<len;i++){
        while(str[i]!='\0') str[j++]=str[i++];
    }
    str[j]='\0';
}

- Anonymous May 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

void rmoveSingleOccurence(char * str, char c){
    int len = strlen(str);
    
    // mark all single occurrences as \0
    for(int i =0; i<len;i++){
        if(str[i]==c){
            //check for next char
            if(str[i+1]==c) { while(str[i+1]==c) i++;}
            else str[i]='\0';
        }
    }
    
    // compress the array
    int j=0;
    for(int i=0; i<len;i++){
        while(str[i]!='\0') str[j++]=str[i++];
    }
    str[j]='\0';
}

- Honey May 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <iostream>
using namespace std;

int main()
{
	
	string a = "baabnmamnaaaanmaj";
	int n=a.length();
	int flag=0;
	char ch;
	
//	cin>>ch;

	ch='a';
		
	for(int i=0;i<n;i++)
	{
		
		if(a[i] != ch)
			{
				if(flag>1)
					cout<<ch<<endl;
				cout<<a[i]<<endl;
				flag=0;
			}
	
		else 
			{			
				if(flag) 
				cout<<a[i]<<endl;
			
				flag++;
			}
			
	}
}

- LPOOK May 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

//c# code
namespace removeChar
{
    public partial class _Default : System.Web.UI.Page
    {
        const string EXAMPLE = "120jdvj00ncdnv000ndnv0nvd0nvd0";
        public List<Char> ExampleList = new List<Char>();

        public char charZero = '0';

        protected void Page_Load(object sender, EventArgs e)
        {

            foreach (char c in EXAMPLE)
            {
                ExampleList.Add(c);
            }
            for (int index = 0; index <= ExampleList.Count - 1; index++)
            {

                if ( ((index + 1) <= ExampleList.Count - 1) && 
                     (index - 1) >= 0
                    )
                {
                    CompareChar(index);
                }
                else
                    if (index == 0 && 
                        ExampleList.ElementAt<Char>(index) == charZero && 
                        ExampleList.ElementAt<Char>(index + 1) != charZero)
                    {
                            RemoveFromList(index);
                    }
                    else
                    {
                        if (index == ExampleList.Count - 1 && ExampleList.ElementAt<Char>(index) == charZero)
                        {
                            RemoveFromList(index);
                        }
                    }
            }
            //var mike = ExampleList;
        }

        public void CompareChar(int index)
        {
            if (
                //NewExampleArray[i] == charZero &&
                 ExampleList.ElementAt<Char>(index) == charZero &&
                 ExampleList.ElementAt<Char>(index + 1) != charZero &&
                 ExampleList.ElementAt<Char>(index - 1) != charZero
            )
            {
                //remove
                RemoveFromList(index);
            }
            //return ExampleList;
        }

        public void RemoveFromList(int index)
        {
            ExampleList.RemoveAt(index);
        }
    }
}

- Anonymous May 29, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Here is c# code but easily convert into C code too.

public static string SimultaneousCharRemove(char[] s)
        {
            int length = s.Length; // Can easily write finding out length of the string
            int NoOfRemovedChar = 0;
            for (int i = 0; i < length - NoOfRemovedChar; i++)
            {                
                if (s[i] == '0' && s[i+1] == '0')
                {
                    int count = 0;
                    for (int j = i + 1; s[j] == '0' && j < length; j++) 
                    {
                        count++;NoOfRemovedChar++;//find the no of extra simultaneous occurances
                    }
                    for (int k = i + 1; k < length - NoOfRemovedChar; k++)
                        s[k] = s[k + count]; // shift the elements to left
                    i = i + count;
                }
            }
            for (int i = length - 1; i >= length - NoOfRemovedChar; i--)
            {
                s[i] = '\0'; //Make null at String end for NoOfRemovedchar elements
            }
            return new string(s);
        }

- kdirishkumar May 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

Very simple approach.Just copy string into another by excluding single appearence of 0 or any specified character.
Just correct me if I am not wrong my code for it is as below

#include<stdio.h>
int i,j;
int main()
{
  char a[100],b[100];
  gets(a);
  int len=strlen(a);
  for(i=0;i<len;i++)
  {
    if(a[i]=='0')
    {
      if(a[i+1]=='0'||a[i-1]=='0')
      {
        b[j]=a[i];
        j++;       
        continue;        
      } 
      else
      {
       continue;    
      }
    }
    b[j]=a[i];
    j++;
  }
  puts(b);
  getch();
  return 0;    
    
}

- Anonymous May 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>

int main(void) {
	int i,j;
	char str[50];
	char ch;
	gets(str);
	ch=getchar();
	for(i=0;str[i]!=NULL;i++){
		if(str[i]==ch&&(str[i+1]!=ch&&str[i-1]!=ch)){
			for(j=i;str[j]!=NULL;j++){
				str[j]=str[j+1];
			}
		}
	}
	printf("Final String %s",str);
	return 0;
}

- rajat sadh May 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

"Access voilation writting location " Exception has been shown while running the above program. To avoid that i have used another string.

#include<stdio.h>
char * removeCharfromStr(char *s,char c)
{
	int pos=0,i=0,cnt;
	char *t; 
	t = (char *)malloc(sizeof(s)+1);
	while(s[i])
	{
		if(s[i]!=c)
		{
			t[pos++]= s[i++];
		}
		else
		{
			cnt = 1;
			for(++i;s[i]&&s[i]==c;++i,++cnt);
			if(cnt >1)
			{
				for(;cnt;cnt--)
					t[pos++] = c;
			}
		}
	}
	t[pos] = '\0';
	return t;
}
int main()
{
	char *s = "120jdvj00ncdnv000ndnv0nvd0nvd0";
	printf("%s",removeCharfromStr(s,'0'));
	return 0;
}

- Ramabrahmam May 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

char* RemoveSingleOccurance( char *string, char chk){

int i,count=0,j=0;
int len;
char *ptr,*cpyptr;
len=strlen(string);
printf("%d\n",len);
ptr = (char *) malloc( sizeof( char)* len);
for( i=0; i< len; i++) ptr[i]='0';
for( i =0; i < len ; i++){
if ( i==0){
if ( (string[i]== chk) && (string[i+1] != chk)){count++; ptr[i]='1';}//printf("cont=%d,ptr[%d]\n",count,i);}
}
else if ( i == len-1){
if ( (string[i]==chk) && (string[i-1] != chk)) {count++; ptr[i]='1';}//printf("cont=%d,ptr[%d]\n",count,i);}
}
else{
if ( (string[i]== chk) && (string[i-1] != chk)&& (string[i]== chk) && (string[i+1] != chk)) {count++; ptr[i]='1';}//printf("cont=%d,ptr[%d]\n",count,i);}
}
}
//printf("%d\n",count);
//for( i=0; i< len; i++)printf("%c",ptr[i]);
//printf("\n");
cpyptr = (char *) malloc( sizeof( char)* (len-count));
for( i=0 ;i <len; i++){
if(ptr[i] !='1')cpyptr[j++]=string[i];

else
continue;
}
delete (ptr);
return( cpyptr);



}

main()
{
char *ptr,*result;
char chk;
int i;
ptr = "0120jdvj00ncdnv000ndnv0nvd0nvd0";
chk='0';
result= RemoveSingleOccurance( ptr, chk);
printf("string=%s\nresultString=%s\n",ptr,result);
scanf("%c\n",&chk);

}}

- sumit May 30, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes
{{{#include <stdio.h>}}} {{{#include <string.h>}}} {{{#include <stdlib.h>}}} {{{char* RemoveSingleOccurance( char *string, char chk){}}} {{{ int i,count=0,j=0;}}} {{{ int len;}}} {{{ char *ptr,*cpyptr;}}} {{{len=strlen(string);}}} {{{printf("%d\n",len);}}} {{{ptr = (char *) malloc( sizeof( char)* len);}}} {{{for( i=0; i< len; i++) ptr[i]='0';}}} {{{for( i =0; i < len ; i++){}}} {{{ if ( i==0){}}} {{{ if ( (string[i]== chk) && (string[i+1] != chk)){count++; ptr[i]='1';}//printf("cont=%d,ptr[%d]\n",count,i);} } {{{ }}}} {{{else if ( i == len-1){ }}} {{{ if ( (string[i]==chk) && (string[i-1] != chk)) {count++; ptr[i]='1';}//printf("cont=%d,ptr[%d]\n",count,i);} }}} {{{} }}} {{{else{}}} {{{ if ( (string[i]== chk) && (string[i-1] != chk)&& (string[i]== chk) && (string[i+1] != chk)) {count++; ptr[i]='1';}//printf("cont=%d,ptr[%d]\n",count,i);} }}} {{{}}}} {{{}}}} {{{//printf("%d\n",count);}}} {{{//for( i=0; i< len; i++)printf("%c",ptr[i]);}}} {{{//printf("\n");}}} {{{cpyptr = (char *) malloc( sizeof( char)* (len-count)); }}} {{{for( i=0 ;i <len; i++){}}} {{{ if(ptr[i] !='1')cpyptr[j++]=string[i];}}} {{{else}}} {{{ continue;}}} {{{ }}}} {{{delete (ptr);}}} {{{return( cpyptr);}}} {{{}}}} {{{main()}}} {{{{}}} {{{char *ptr,*result;}}} {{{ char chk;}}} {{{int i;}}} {{{ptr = "0120jdvj00ncdnv000ndnv0nvd0nvd0";}}} {{{chk='0';}}} {{{result= RemoveSingleOccurance( ptr, chk);}}} {{{printf("string=%s\nresultString=%s\n",ptr,result);}}} {{{scanf("%c\n",&chk);}}} {{{}}}} - sumit..corrected code... May 30, 2014 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

#include<stdio.h>
int main()
{
char str[100] = "120jdvj00ncdnv000ndnv0nvd0nvd0";
int i=0;
while(str[i]!='\0')
{
if(str[i]!='0')
printf("%c",str[i]);
else if(str[i+1]=='0')
printf("%c%c",str[i],str[i+1]);
i++;
}
return 0;
}

Output: 12jdvj00ncdnv0000ndnvnvdnvd

- Sudeep June 02, 2014 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not go well with odd 0's like 000, 00000 etc.

- bhallavaibhav September 23, 2014 | 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