You have a string which stores a number with commas. For example, a string that has the number 345,000,000. How will you manipulate this string in-place [without using any extra memory] so that the output is the original string without any commas in O(n) ?
Code:int nLen = strlen(str); char* pWriteTo = str; char* pReadFrom = str; for (int i = 0; i < nLen; i++) { if (*pReadFrom != ',') { *pWriteTo = *pReadFrom; pWriteTo++; } pReadFrom++; }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks