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.
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)
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.
@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.