Cisco Systems Interview Question
Software Engineer / DevelopersTeam: NOSTG
Country: India
Interview Type: In-Person
public static String replace(String str, String s1, String s2){
str = str.replaceAll(s1, s2);
return str;
}
void *strrp(char *src,char *sub,char *rp,char *p)
{
int sub_len=strlen(sub);
char *po=NULL,*q=src;
while((po=strstr(q,sub))!=NULL)
{
strncat(p,q,po-q);
strcat(p,rp);
q+=po-q+sub_len;
}
strcat(p,q);
}
Should the output be what is stated in the question above
OR
// replace s1 with s2 in place and expand original string
"Hi i am pqrstuv and i am in pqrstuv"
OR
// best effort replacement of s2, without expanding original string
"Hi i am pqrstuv i am in pqr"
We have make sure the following :
1. 'str' is big enough to accommodate replace string, as the replace string can be bigger than the original string.
2. 'str' is not a constant.
int findAndReplace(char *str,int strSize,char *find,char *replace)
{
char *found;
int lenStr = strlen(str);
int lenFind = strlen(find);
int lenReplace = strlen(replace);
char *endStr = str + lenStr;
char *endStrMax = str + strSize;
int lenDiff;
int ret = 0;
while(found = strstr(str,find))
{
if (lenFind == lenReplace)
{
memcpy(found,replace,lenFind);
}
else if (lenFind > lenReplace)
{
memcpy(found,replace,lenReplace);
memcpy(found+lenReplace,found+lenFind, endStr - found - lenFind );
}
else if (lenFind < lenReplace)
{
lenDiff = lenReplace - lenFind;
if (lenStr + lenDiff > strSize)
{
ret = -1; //out of bounds
}
else
{
memcpy(found+lenReplace,found+lenFind,endStr - found - lenFind);
memcpy(found,replace,lenReplace);
lenStr += lenDiff;
endStr += lenDiff;
}
}
}
return ret;
}
Something similar here?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char* argv[])
{
int i;
char arr[100];
char* a;
char* b="abc";
char* c="pqrstuv";
int lenb=strlen(b);
int lenc=strlen(c);
char* ptrb=malloc(lenb);
char* out;
char* outtemp;
fgets(arr,100,stdin);
a=arr;
out=(char*)malloc(5*strlen(a));
outtemp=out;
memset (out,0,strlen(a)+1);
memset (ptrb,0,lenb);
for (i=0;*a;i++)
{
// Copy lenb characters into temporary array
strncpy(ptrb,a,lenb);
// Compare temporary array with reference b
if (!strcmp(ptrb,b))
{
// If match, replace with other string.
strncpy(out,c,lenc);
out+=lenc;
a+=lenb;
}
else
{
// Increment pointers
*out++=*a++;
}
}
// Print output
*out='\0';
fputs(outtemp,stdout);
// Free temporary memory
free(outtemp);
}
implemented in C, with o(n) space and ~o(n) time complex.
char * replaceStr(char *sOriginal, char *sSub, char *sNew)
{
int olen = strlen(sOriginal);
int subLen = strlen(sSub);
int newSubLen = strlen(sNew);
int i, j, k=0;
char *sOutPut = NULL;
sOutPut = (char *)malloc(100);
memset(sOutPut, 0, 100);
memcpy(sOutPut, sOriginal, olen);
printf("%s\n\r", sOutPut);
for(i=0; i<100 && sOutPut[i]!='\0'; i++)
{
for(j=0; j<subLen; j++)
{
if(sOutPut[i+j] == sSub[j])
{
}else
{
break;
}
}
if(j==subLen)
{
//find a substring, then replace sSub with sNew
memcpy(&sOutPut[i+newSubLen], &sOriginal[k+subLen], olen-k-subLen+1);
memcpy(&sOutPut[i], sNew, newSubLen);
i = i+newSubLen-1;
k = k+subLen;
}else
{
k++;
}
}
printf("%s\n\r", sOutPut);
}
#include <iostream>
#include <cstring>
using namespace std;
void replace(char *s, char *s1, char *s2, char *d){
char *r = strstr(s, s1);
strncpy(d, s, r-s);
strcat(d, s2);
while(*r != ' ' && *r != '\0') r++;
strcat(d, r);
}
int main(){
char *s = "Hello this damn World!";
char *s1 = "damn";
char *s2 = "nice";
char *d = new char[80];
replace(s, s1, s2, d);
cout<<s<<endl;
cout<<d<<endl;
}
works only for first occurrence. Fails for this.
#include <iostream>
#include <cstring>
using namespace std;
void replace(char *s, char *s1, char *s2, char *d){
char *r = strstr(s, s1);
strncpy(d, s, r-s);
strcat(d, s2);
while(*r != ' ' && *r != '\0') r++;
strcat(d, r);
}
int main(){
char *s = "Hello this damn World! damn";
char *s1 = "damn";
char *s2 = "nice";
char *d = new char[80];
replace(s, s1, s2, d);
cout<<s<<endl;
cout<<d<<endl;
}
OP:
Hello this damn World! damn
Hello this nice World! damn
it should be :
Hello this damn World! damn
Hello this nice World! nice
package string_replace;
import java.util.StringTokenizer;
public class Strrep {
static String strrep (String str, String s1, String s2) {
StringBuilder sb = new StringBuilder();
StringTokenizer st = new StringTokenizer(str, " ");
while (st.hasMoreTokens()) {
String word =st.nextToken();
if (word.equals(s1)) {
word=s2;
}
sb.append(word);
sb.append(" ");
}
return sb.toString();
}
public static void main (String[] args) {
String str=args[0];
String s1=args[1];
String s2=args[2];
System.out.println("Original string is "+str);
String str2=strrep(str,s1,s2);
System.out.println("New string is "+str2);
}
}
Use the Knuth algorithm to find the occurrence(s) of the s1 in str1 and replace every such occurrence once found. Knuth algorithm has a complexity of O(strlen(str1)) plus a O(strlen(s1)) pre-proccessing. Replacing will take maximum of O(strlen(s1)).
public String findBigStringAndReplace(String str, String str2) {
StringTokenizer st = new StringTokenizer(str, ".");
int bigLength = 0;
String bigWord = "";
while (st.hasMoreTokens()) {
String word = st.nextToken();
int wordLength = word.length();
if (word.length() > bigLength) {
bigLength = wordLength;
bigWord = word;
}
}
return str.replaceAll(bigWord, str2);
}}
public String findBigStringAndReplace(String str, String str2) {
StringTokenizer st = new StringTokenizer(str, " ");
int bigLength = 0;
String bigWord = "";
while (st.hasMoreTokens()) {
String word = st.nextToken();
int wordLength = word.length();
if (word.length() > bigLength) {
bigLength = wordLength;
bigWord = word;
}
}
return str.replaceAll(bigWord, str2);
}
// Pavan Kumar Naidu
#include <stdio.h>
#include <string.h>
char *replace_str(char *str, char *orig, char *rep)
{
static char buffer[4096];
char *p;
if(!(p = strstr(str, orig))) // Is 'orig' even in 'str'?
return str;
strncpy(buffer, str, p-str); // Copy characters from 'str' start to 'orig' st$
buffer[p-str] = '\0';
sprintf(buffer+(p-str), "%s%s", rep, p+strlen(orig));
return buffer;
}
int main(void)
{
puts(replace_str("Hello, world!", "world", "Miami"));
return 0;
}
public String replaceString(String str, String str1, String str2){
- simranbahri1 November 16, 2012String[] strArr = str.split(" ");
String Buffer finalString = new StringBuffer();
for(int i=0;i<strArr.length;i++)
{
if(strArr[i].equals(str1) {
if(i==0)
finalString.append(str2);
else{
finalString.append(" ");
finalString.append(str2);
}
}else{
if(i==0)
finalString.append(strArr[i]);
else{
finalString.append(" ");
finalString.append(strArr[i]);
}
}
}
return finalString.toString();
}