Tag Archives: string

Here is the world famous how many characters in this C++ string job interview question?

Here is the world famous how many characters in this C++ string job interview question?

Also, here is a way to find the number of words in a string (same as count number of specific characters) This always reports 1 more than so maybe display count – 1?

#include
#include
using namespace std;

int main()
{
int i, numspaces;
char nextChar;
string msg;

numspaces=1;

cout << "Type in a string\n"; getline(cin, msg); // checks each character in the string for (i=0; i NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

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

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:

#include
#include
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!

How to convert string to double dollars using C++ string stream, a STL container, and locale

How to convert string to double dollars using C++ string stream, STL containers, and locale

#include
#include
#include
#include
#include

using namespace std;

void to_dollar(int a);

int my_number_format(int num)
{
int idx;
char buffer[64];

idx = sizeof(buffer)-1;
buffer[idx–] = 0; // end zero

int count = 0;
while (num > 0) {
if (0 == idx)
return -1;
if (count > 2) {
buffer[idx–] = ‘,’;
if (0 == idx)
return -1;
count = 0;
}
count++;
buffer[idx–] = ‘0’+(num % 10);
num /= 10;
}

buffer[idx] = ‘$’; // currency sign
return printf(“%s\n”, &buffer[idx]);
}

int main()
{
int userInput;

cout << "Please enter an integer-:> ” << endl; cin >> userInput;
int result;
result=my_number_format(userInput);
return (0);
}

void to_dollar(int a)
{
stringstream inString;
string strTemp;
long double intTemp;

inString << a; // convert integer to string for easier handling inString.str(inString.str()+"00"); // concatenate two zeos to denote currency fractional digits inString >> intTemp; // convert back to integer

locale loc(“english_USA”); // set the locale
cout.imbue(loc);

cout.setf(cout.flags() | ios::showbase); // turn on currency sign printing
const money_put& m_put = use_facet >(loc); // get the money_put facet
m_put.put(cout, false, cout, ‘ ‘, intTemp); // show me the money. Note to use fractionally adjusted value.
cout << endl; }

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!