My program reads a string of characters. Prints all occurrences of letters and numbers, sorted in alphabetical and numerical order. Letters will be converted to uppercase.Other characters are filtered out, while number of white spaces are counted.
Solution you have provided works like a charm.... I just needed to change the loop to work it properly as it was not going to the end of the input string, maybe it was typo or you did not test your code....
the inner loop J goes up to len_newstr not len_newstr -1
1 2 3 4 5 6 7 8 9 10 11 12 13
for ( int i = 0; i < /*strlen(newStr) - 1*/ len_newstr - 1 ; i++)
{
for ( int j = i + 1; j < /* strlen(newStr) */ len_newstr; j++)
{
if (newStr[j] < newStr[i]) // sorts in alphabetical
{ // and numerical order
constchar temp = newStr[i];
newStr[i] = newStr[j];
newStr[j] = temp;
}
}
}
constint len_str = pStr - str ; // *** added (can you figure out why?) by the way I couldn't figure it out? would you please explain it to me?
> I just needed to change the loop to work it properly as it was not going to the end of the input string,
> maybe it was typo or you did not test your code....
It was not a typo.
A genuine programming error which was not caught because the code wasn't peer-reviewed or tested.
> constint len_str = pStr - str ; // *** added (can you figure out why?)
> by the way I couldn't figure it out? would you please explain it to me?
At the end of this loop constchar* pStr = str; while (*pStr != '\0') { /* ... */ pStr++; } - pStr points to the terminating null character of the string.
pStr - str therefore gives the number of characters between the terminating null character and the beginning of the string, which is the length of the string.
(Array str is implicitly converted to a pointer to the first element of the array).