Facebook Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: In-Person
/* Write a funtion that finds all the different ways you can split up a word
into a concatenation of two other words. */
#include <stdio.h>
#include <string.h>
void getleftstring(char *str,int index, char *leftstring);
void getrightstring(char *str,int index, char *rightstring);
int main()
{
char str[64];
int index = 0;
int len = 0;
char leftstring[64], rightstring[64];
printf("Enter the word:");
scanf("%s",str);
len = strlen(str);
for(index = 0; index < len-1; ++ index)
{
memset(leftstring,0, 64);
memset(rightstring,0, 64);
getleftstring(str,index,leftstring);
getrightstring(str,index,rightstring);
printf("%s : %s\n", leftstring, rightstring);
}
return 0;
}
void getleftstring(char *str,int index, char *leftstring)
{
char tempStr[64];
int i = 0;
memset(tempStr,0,64);
for(i = 0; i <= index; ++i)
tempStr[i] = str[i];
tempStr[i] = '\0';
strcpy(leftstring,tempStr);
}
void getrightstring(char *str,int index, char *rightstring)
{
char tempStr[64];
int i = 0;
int len = strlen(str);
memset(tempStr,0,64);
for(index = index+1; index < len; ++index)
tempStr[i++] = str[index];
tempStr[i] = '\0';
strcpy(rightstring,tempStr);
}
- Anonymous September 12, 2012