N/A Interview Question
xyzsCountry: United States
Interview Type: Written Test
#include<stdio.h>
#include<string.h>
void translate(char astring[40])
{
int i=0;
int k,j;
k=strlen(astring);
while(astring[i]!='\0')
{
if(astring[i]=='A'&&astring[i+1]=='B')
{
astring[i]='C';
j=i;
while(j<k)
{
astring[j+1]=astring[j+2];
j++;
}
}
i++;
}
}
int main()
{
char astring[40];
printf("enter string");
gets(astring);
printf("original string was:%s\n",astring);
translate(astring);
printf("string is %s",astring);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
void translate(char*);
int main()
{
char str[20];
printf("enter any string");
fgets(str, 20, stdin);
translate(str);
printf("\nthe translated string is \n");
printf("\n%s", str);
}
void translate(char* str)
{
if(str[0]=='\0')
return;
int i=1, j;
while(str[i]!='\0')
{
if(str[i-1]=='A'&&str[i]=='B')
{
str[i-1]='C';
for(j=i;str[j]!='\0'; j++)
{
str[j]=str[j+1];
}
i--;
}
i++;
}
return;
}
I was given this one last week as well. Only took a couple minutes to write a base version but accidentally passed a char* through the function (which doesn't work) instead of the 'char astring[]' mentioned in the example lol. Here is the code I submitted and what I wanted it to be.
Turned in:
what it would have ended up as...
Like I said, I passed a char pointer instead of an array when testing it. By the time I figured that out, I didn't have enough time to make the modifications. Downside to a two step programming process I guess. Bummer.
- gscottiv December 16, 2015