Directi Interview Question
Country: United States
#include <iostream>
#include <cstring>
using namespace std;
void replaceDuplicate(char *a) {
int len = strlen(a);
int count = 1;
int iReplace = 0;
char lastChar = '\0';
for (int i = 0; i <= len; i++) {
char c = a[i];
if (c == lastChar) {
count ++;
} else {
if (count > 1) {
a[iReplace++] = lastChar;
a[iReplace] = 48 + count;
count = 1;
}
lastChar = c;
iReplace++;
}
}
a[iReplace] = '\0';
}
int main(void) {
char a[100];
cin >> a;
replaceDuplicate(a);
cout << a;
return 0;
}
a[iReplace] = 48 + count;
vs
a[iReplace] = '\0';
Don't you feel that something is wrong with this?
You mean 48 + count may be out of range of char? It would be more understandable by replacing it with '0' + count.
#include "stdlib.h"
#include "string.h"
#include "stdio.h"
int main()
{
char str[]="aaaabbbccccdd";
int length=strlen(str)-1;
int i=0,count=0,k=0;
printf("befor conversion--> %s",str);
for(k=0;k<=length;k++)
{
if(str[i]!=str[k])
{
str[i+1]=count+'0';
i=i+2;
str[i]=str[k];
count=1;
}
else
{
count++;
}
}
str[i+1]=count+'0';
str[i+2]='\0';
printf("\nafter conversion--> %s",str);
return 1;
}
#include <stdio.h>
#include <stdlib.h>
int arr[100];
void count_seq(char *p)
{
int count =1;
for(;*p;p++){
if(p[1] != '\0' && (p[0] == p[1]) )
{
count++;
}
else {
if(!(p[1]))
printf("%c%d",p[0],count);
else{
printf("%c%d",p[0],count);
count =1;
}
}
}
}
int main()
{
printf("Enter the string\n");
gets(arr);
count_seq(arr);
return 0;
}
In place compression, I did not append the count to the character if it exists only once
public char[] InPlaceCompress(string input)
{
char[] inputInChar = input.ToCharArray();
if (inputInChar.Length == 0)
return null;
char prevCharacter = inputInChar[0];
int count = 1;
int changeIndex = 0;
for (int i = 1; i < inputInChar.Length; i++)
{
if (prevCharacter == inputInChar[i])
{
count++;
}
else
{
inputInChar[changeIndex] = prevCharacter;
changeIndex++;
if (count > 1)
{
char[] countAsChars = count.ToString().ToCharArray();
for (int j = 0; j < countAsChars.Length; j++)
{
inputInChar[changeIndex] = countAsChars[j];
changeIndex++;
}
count = 1;
}
prevCharacter = inputInChar[i];
}
}
inputInChar[changeIndex] = prevCharacter;
changeIndex++;
if (count > 1)
{
char[] countAsChars = count.ToString().ToCharArray();
for (int j = 0; j < countAsChars.Length; j++)
{
inputInChar[changeIndex] = countAsChars[j];
changeIndex++;
}
}
for (int k = changeIndex; k < inputInChar.Length; k++)
{
inputInChar[k] = '\0';
}
return inputInChar;
}
tested with some inputs
input: abbbbbbbbbbbbbbbbbbbbbbbbbbccccddddd
output: ab26c4d5
input: aaaaaabbbbbccccd
output: a6b5c4d
input: abcd
output: abcd
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
//void worldcount(char* );
void main()
{
char str[19];
//="aabbbcccc";
int i,len, count=1;
char temp;
printf("enter the string");
gets(str);
len=strlen(str);
for (i=0;i<len;i++)
{
temp=str[i];
if (temp==str[i+1])
count++;
else
{
printf("%c%d",str[i],count);
count=1;
}
}
printf("\n");
}
void countChars(char * inString){
if(inString == NULL)
return;
int start = 0;
int index = 0;
int insert = 0;
int count = 0;
while(inString[index] != NULL){
while(inString[index] == inString[start]){
index++;
}
inString[insert] = inString[start];
insert++;
count = index - start;
if(count > 1){
insert += sprintf(inString[insert], "%d", count);
}
index++;
start = index;
}
if(insert != 0 && insert < strlen(inString))
inString[insert] = NULL;
}
#include<cstdio>
#include<cstring>
#include<cstdlib>
int main()
{
char a[100];
scanf("%s",a);
int i,t,len = strlen(a);
for(i=1,t=0;i<len;i++)
{
if(a[i] != a[t])
{
if(i-t > 1)
{
a[i-1] = i-t;
if(a[i-1] > 9)
{
a[i-2] = 127;
}
}
t = i;
}
}
if(t==0)
{
if(len<10)
{
a[1] = len + 48;
a[2] = 0;
}
else
{
a[1] = len/10 + 48;
a[2] = len%10 + 48;
a[3] = 0;
}
printf("%s",a);
exit(0);
}
else
{
if(len-t>1)
{
a[len-1] = len - t;
if(len - t > 9)
{
a[len-2] = 127;
}
}
}
int c;
for(i=0,c=0;i<len;i++)
{
if(a[i] < 10)
{
c = c+ a[i] - 2;
}
else if(a[i] == 127)
{
c = c + a[i+1] - 3;
}
a[i-c] = a[i];
}
int nlen = len - c;
a[nlen] = 0;
for(i=0;i<nlen;i++)
{
if(a[i] < 10 )
{
a[i] = a[i] + 48;
}
else if(a[i] == 127)
{
a[i] = a[i+1]/10 + 48;
a[i+1] = a[i+1]%10 + 48;
}
}
for(i=0,c=0;i<nlen-1;i++)
{
if(a[i]>96 && a[i+1] > 96)
{
c++;
}
}
if(a[nlen-1] > 96)
{
c++;
}
int offset = c;
if(len<nlen+c || c==0)
{
printf("%s",a);
exit(0);
}
if(a[nlen-1] > 96)
{
a[nlen+c-1]=49;
c--;
a[nlen+c-1] = a[nlen-1];
}
for(i=1;i<nlen;i++)
{
if(a[nlen-1-i] > 96 && a[nlen-i] > 96)
{
a[nlen+c-1 - i] = 49;
c--;
}
a[nlen+c-1 - i] = a[nlen-1 - i];
}
a[nlen+offset] = 0;
printf("%s",a);
}
As far as I can think this will work, absolutely Inspace. If it doesnt work for some testcases give me those cases. Thank You in Advance
//program to replace char with their count
#include<iostream>
#include<cstring>
#define SIZE 100
using namespace std;
int main()
{
char a[SIZE];
cin>>a;
int pos=0,count=1,i=0;
for(int k=1;a[k]!='\0';k++)
{
if(a[k]==a[i])
{
count++;
}
else
{
a[pos] = a[i];
if(count>9)
{
a[++pos]='0'+count/10;
count = count%10;
}
a[++pos]='0'+count;
pos++;
i=k;
count=1;
}
}
a[pos]=a[i];
if(count>9)
{
a[++pos]='0'+count/10;
count = count%10;
}
a[++pos] = '0'+count;
a[++pos] = '\0';
cout<<a;
return 0;
}
char a[10] = "aabbccc";
int main()
{
char ch;
int read = 0, write = 0;
int curr_count = 0;
a[strlen(a)] = '\0';
while(a[read] != '\0' ) {
ch = a[read];
a[write] = ch;
curr_count = 1;
read++;
while(a[read] == ch) {
curr_count++;
read++;
}
a[++write] = (int) ('0' + curr_count);
printf("a = %s, ch = %c ; a[read] = %c ; a[write] = %c \n", a, ch, a[read], a[write]);
write++;
}
a[write] = '\0';
printf("%s \n", a);
return 1;
}
A few comments:
1. Why don't you write a function to do it a make main a test-bed for it?
2. Why main is returning 1??? What is the return value of main supposed to be? (It is left to the reader to answer this question)
3. Why don't you handle/test for misformed texts? E.g. "alma"? This cannot be converted inspace.
4. Your code does not handle the case when a character appears more than 9 times.
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{char* str="aabbbcccaabbccc";
int* ptr=(int*) calloc(256,sizeof(int));
ptr[*str]++;
for(int i=1;i<strlen(str);i++)
{ptr[*(str+i)]++;
if(*(str+i)!=*(str+i-1))
{cout<<*(str+i-1)<<ptr[*(str+i-1)];
ptr[*(str+i-1)]=0;
}
}
if(ptr[*(str+strlen(str)-1)]==1)
cout<<*(str+strlen(str)-1)<<ptr[*(str+strlen(str)-1)];
delete ptr;
getch();
return 0;}
//Output: a2b3c3a2b2c3
other test cases: input str=aabbbcccaabbc
output=a2b3c3a2b2c1
- gurumukhi October 26, 2012