Character sequence (Char arr[])

I have a character sequence with alphabets, integers and punctuations. After a for loop check, I separate the alphabets and store them in a new character sequence, but I cannot. I have tried various methods such as strcat, strcpy and currently trying a nested for loop method to store the value in new char arr[] but I am getting a logical error. I am new to C++ so I will be grateful if anyone can help me out.

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
#include <cctype>
#include <iostream>
#include <cstring>
#include <string.h>
using namespace std;
namespace foo {int count =0; char str[] = "ad138kw+~!$%?';]qjj";}
int main()
{
using foo::count;
using foo::str;
char str1[20];
    for(int i=0;i<=strlen(str);i++){
        char current=str[i];
        if(isalpha(str[i])){
            if(isupper(current) ? tolower(current) : current){
                count++;
                char temp=str[i];
                for(int j=0;j<strlen(str);j++){
                    if(str1[j]!=str[i]){
                        str1[j]=temp;
                    }
                }
            }
        }
        else if(ispunct(str[i])){
            cout<<'1'<<"\n";
        }
    }
cout<<str1<<"\n";
cout<<"\t"<<count;
return 0;
}

Last edited on
This might be useful.
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
#include <iostream>
#include <cctype>

int main()
{

    char str[] = "ad138kw+~!$%?';]qjj";
    int length = sizeof(str)/sizeof(char);

    char* alphabet_character_array = new char[length];

    char current{};
    int count = 0;

    for(int i = 0; i < length; i++)
    {
        current = str[i];
        if(isalpha(str[i]))
        {
            alphabet_character_array[count] = current;
            count++;
        }
    }
    std::cout << alphabet_character_array << '\n';
    std::cout<< "Count: " << count << '\n';

    return 0;
}


adkwqjj
Count: 7
Program ended with exit code: 0
Okay so this works; I need to know when you first declared alpahbet_character_array you used char*, may I know the reasoning behind using pointer.

Also, in line 8 you divided the size of str and char to find length cant we just determine the length of the new array by using the count.
Last edited on
Consider:

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
#include <iostream>
#include <iterator>
#include <cctype>

int main()
{
	const char str[] {"ad138kw+~!$%?';]qjj"};
	auto alpha {new char[std::size(str)]{}};
	auto punc {new char[std::size(str)]{}};
	size_t cntAlpha {};
	size_t cntPunc {};

	for (const unsigned char ch : str)
		if (isalpha(ch))
			alpha[cntAlpha++] = ch;
		else if (ispunct(ch))
			punc[cntPunc++] = ch;

	std::cout << alpha << '\n';
	std::cout << "Count: " << cntAlpha << '\n';
	std::cout << punc << '\n';
	std::cout << "Count: " << cntPunc << '\n';

	delete[] alpha;
	delete[] punc;
}



adkwqjj
Count: 7
+~!$%?';]
Count: 9

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cctype>

int main()
{
   char str[] = "ad138kw+~!$%?';]qjj";
   int num = 0;
   for ( char *p = str; *p; p++ ) if ( isalpha( *p ) ) num++;

   char *alphabetic = new char[num+1];
   for ( char *p = str, *q=alphabetic; *p; p++ ) if ( isalpha( *p ) ) *(q++) = tolower( *p );
   alphabetic[num] = '\0';                  // remember to null-terminate
   
   std::cout << alphabetic << '\n';
   std::cout << "Count: " << num << '\n';
   
   delete [] alphabetic;
}
Last edited on
null-terminate isn't explicitly needed if the memory is default initialised when allocated.
Okay so this works; I need to know when you first declared alpahbet_character_array you used char*, may I know the reasoning behind using pointer.


C style strings (what you are using) are either pointers or arrays. The difference between pointers and arrays can be a little fuzzy because arrays degrade into pointers (implicit casting).
so he could have said
char str[] = "walloftext"; //for your code sample there is no difference between this and char*
which was shown to you above but without any comments.

and str will work in any of the C string functions (eg printf, strstr, strcpy, etc) directly:
strcpy(other, str); //str degrades to char* here and works

How you get the length of the string is not too critical and the technique he showed is handy so you will want to remember it for times when you have no better choice (eg an array of doubles may be hard to tell junk from data by other methods, C strings have that handy 0 last entry)


Last edited on
Also range-for and std::size() works for char str[] when the size of the array is known (ie not passed as a function param as a pointer via degradation). They don't work for char* For these to work for a char array passed via a function, the char array needs to be specified as a templated ref so that the size can be obtained.
Last edited on
@OP 1000 pardons for the delay - internet failed locally.

I need to know when you first declared alpahbet_character_array you used char*, may I know the reasoning behind using pointer.

I chose to use a dynamic array because in the general case of any initial string size the list of array characters might be longer than your choice of 20. I decided to use the size the same as the length of the original string. That's the reason for the pointer and new.
(There should be a corresponding delete[] to clean up at the end of main in this case)
If you want to follow it up 'dynamic arrays' are explained at https://www.learncpp.com/cpp-tutorial/dynamically-allocating-arrays/

You could use an STL container like a <vector> but that's another story.

Also, in line 8 you divided the size of str and char to find length cant we just determine the length of the new array by using the count.

Either way will give you the info you need to determine the length of the initial string and the (maximum) size of the output string. count won't do that because the full count hasn't occurred at that point in the program.

I've added a few bits to the original to demonstrate, not the difference of 1 in length due to the hidden delimiter at the end of the initial string.

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
#include <iostream>
#include <cctype>

int main()
{
    char str[] = "ad138a*&kw+~!$%?';]qjj"; // <--
    int length = sizeof(str)/sizeof(char);

    std::cout << length << ' ' << strlen(str) << '\n';

    char* alphabet_character_array = new char[length]{};

    char current{};
    int count = 0;

    for(int i = 0; i < length; i++)
    {
        current = str[i];
        if(isalpha(str[i]))
        {
            alphabet_character_array[count] = current;
            count++;
        }
    }
    std::cout << alphabet_character_array << '\n';
    std::cout<< "Count: " << count << '\n';

    for(int i = 0; i < length; i++)
    {
        std::cout << (int)alphabet_character_array[i] << ' '; 
        // CONVERT TO int's TO SHOW 0's

    }

    delete [] alphabet_character_array; // <--

    return 0;
}


23 22
adakwqjj
Count: 8
97 100 97 107 119 113 106 106 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Program ended with exit code: 0
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cctype>

int main()
{
   char str[] = "ad138kw+~!$%?';]qjj";
   int num = 0;
   for ( char *p = str; *p; p++ ) if ( isalpha( *p ) ) num++[str] = tolower( *p );
   num[str] = 0;
   std::cout << str << '\n' << "Count: " << num << '\n';
}
Last edited on
Nice syntax! :)
seeplus wrote:
Nice syntax! :)


Actually, it's awful. But I thought that since the OP had more than enough good solutions to be getting on with, it might be time to play ...!
Last edited on
I was being sarcastic..... :)

It's such a long time since I've come across that syntax that I had to do a double take to realise what it was.
Last edited on
Some of it comes from Stroustrup's book, and I had to ask for an explanation in this forum (kindly supplied by @Peter87) to understand it:
http://www.cplusplus.com/forum/general/217519/#msg1005591

However, Stroustrup does recommend that it not be used in real code ...
Last edited on
Topic archived. No new replies allowed.