Sorting alphabets and numbers

Hi!

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.

the problem is it crushes when i run the program

here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
const int SIZE = 100;

using namespace std;


int main()
{
	char *pStr, str[SIZE] = "", newStr[SIZE] = "", ch;
	int count = 0, i = 0, j;

	cout << "Enter a number of mixed characters: ";
	cin.getline(str, SIZE);
	pStr = str;
	
	while (*pStr != '\0')
	{
		if (isalnum(*pStr))
			ch = toupper(*pStr);
			newStr[i++] = ch;
		
		if (*pStr = ' ')
			count++;
		pStr++;
	}
	newStr[i] = '\0';

	cout << strlen(str) - strlen(newStr) << " characters were filtered out"
		<< " out of which " << count << " whitespaces were encountered.\n";
	
	int temp;

	for (i = 0; i < strlen(newStr) - 1; i++);
	{
		for (j = i + 1; j < strlen(newStr); j++);
		{
			if (newStr[j] < newStr[i])	// sorts in alphabetical
			{						// and numerical order 
				temp = newStr[i];			
				newStr[i] = newStr[j];
				newStr[j] = temp;
			}
		}
	}
	cout << "New sorted string: " << newStr << endl;
	
		return 0;
}


I would appreciate if someone helps me out and tell me what is wrong with my solution???

Thanks in Advance
Best Regards
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <algorithm>
#include <iostream>
#include <string>
int main()
{
    std::string hi("iH"); //h is after i
    int arr[5] = {3,1,5,2,4};
    std::transform(hi.begin(), hi.end(), hi.begin(), [](unsigned char c) {return std::tolower(c);});
    std::sort(hi.begin(), hi.end());
    hi[0] = std::toupper(hi[0]);
    std::cout << hi << "\n";
    std::sort(arr, arr+5);
    for(auto a : arr)
    {
        std::cout << a;
    }
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <cctype>
#include <string>
#include <vector>
int main()
{
    std::string A = "My name is iQChange";
    std::vector<int> space_location;
    for(auto a : A)
    {
        if(isspace(a))
        {
            space_location.push_back(std::distance(A.begin(), &a);
        }
    }
    std::cout << "Found " << space_location.size() << " spaces; in: ";
    for(auto a : space_location)
    {
        std::cout << a << "\n";
    }
}

Code not tested.
}
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
const int SIZE = 100;

using namespace std;


int main()
{
    // *** postpone defining variables till they are required,
    // ***  and limit thier visibility to the smallest scope possible
    // char *pStr, str[SIZE] = "", newStr[SIZE] = "", ch;
    // int count = 0, i = 0, j;

    cout << "Enter a number of mixed characters: ";
    char str[SIZE] ; // *** added
    cin.getline(str, SIZE);

    const char* pStr = str; // const, we do not intend to modify str

    char newStr[SIZE] ;
    int i = 0 ;
    int count = 0 ;

    while (*pStr != '\0')
    {
        if (isalnum(*pStr))
        { // *** brace added
            char ch = toupper(*pStr);
            newStr[i++] = ch;
        } // *** brace added

        //if (*pStr = ' ')
        if( isspace(*pStr) )
            count++;

        pStr++;
    }
    newStr[i] = '\0';

    const int len_str = pStr - str ; // *** added (can you figure out why?)
    const int len_newstr = i ; // *** added

    cout << /*strlen(str) - strlen(newStr)*/  len_str - len_newstr << " characters were filtered out"
         << "\n    out of which " << count << " whitespaces were encountered.\n";

    // int temp;

    for ( int i = 0; i < /*strlen(newStr) - 1*/ len_newstr - 1 ; i++) // ; removed
    {
        for ( int j = i + 1; j < /* strlen(newStr) */ len_newstr - 1 ; j++) // ; removed
        {
            if (newStr[j] < newStr[i])	// sorts in alphabetical
            {                           // and numerical order
                const char temp = newStr[i];
                newStr[i] = newStr[j];
                newStr[j] = temp;
            }
        }
    }
    cout << "New sorted string: " << newStr << /*endl*/ '\n' ;

    // return 0; // this is implied
}
Hi JLBorges!

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
                const char temp = newStr[i];
                newStr[i] = newStr[j];
                newStr[j] = temp;
            }
        }
    }


const int 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?

Thank once again for your time and help
Last edited on
> 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.


> const int 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 const char* 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).
Hi !JLBorges !

Bundle of thanks :) very professional
Topic archived. No new replies allowed.