All the potential string related questions asked in a C++ job interview

(Last Updated On: May 18, 2010)

All the potential string related questions asked in a C++ job interview
The differences between C string and the string class in C++ is that C strings are handled by standard function and are more efficient since they are arrays of characters. C++ string class has more encapsulated functionality.
You may get asked about substring replacement but there are usually three options you can choose. You could use start index of a substring, use KMP in O[n]) algorithm, or brute force using O(n*m).
Here is a difficult brute force way:

using namespace std;

int main(int argc, char *argv[]) {

int idx = 0;
char target[100];
char *pTag = target;
char sub[10];
char replace[100];
char *pRpr = replace;
if(argc != 4)
cout << "Exit... Bye Bye\n"; exit(1); } strcpy(target, argv[1]); strcpy(sub,argv[2]); strcpy(replace,argv[3]); //Loop until target 0 char * pTg, * pSb, *pRp; pRp = replace; while(pTag != '\0') { pTg = pTag; pSb = sub; while(*pSb != '\0' && *pTg != '\0' && *pTg++ == *pSb++); if(*pSb == '\0' && *(pTg -1) == *(pSb -1)) { while(*pRp != '\0')pRp++; while((*pRp++ = *pTg++) != '\0'); while((*pTag++ = *pRpr++) != '\0'); cout << target << "\n"; return 0; } pTag++; } return 0; } Here is a much easier sub str str way: #include
using namespace std;

void replace(char *target, char *substring, char *replace);

int main()
char target[100] = “MyNameIsGajanan”;
char *substring = “Gajan”, *rep = “AAA”;
//cout << target <FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

Don't miss out!

You will received instantly the download links.

Invalid email address
Give it a try. You can unsubscribe at any time.


Check NEW site on stock forex and ETF analysis and automation

Scroll to Top