Need copy only alphabetic from a string to new string by themselves.

I think this code fails to operate properly because of some iteration error on my part. Please advise how I may correct this code. And, don't worry, I am not a student. I am doing this because I want to write an old fashioned text-based game. So you may help me if you like. =o)

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
  #include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <ctype.h>
using namespace std;

int main ()
{
int i=0;
char str_ofAlpha[30];
string str1;

cout << "Enter text:\n\n";
getline (cin, str1);

while (str1[i])
{
//This next code works to identify only the alphabetic characters:
if (isalpha(str1[i]))
{
cout << str1[i] << " is alphabetic.\n"; //here is the proof.

//This next code isn't functioning as desired:
size_t length = str1.copy(str_ofAlpha,i,1);
str_ofAlpha[length]='\0';
		
cout << "str_ofAlpha contains: " << str_ofAlpha << '\n'; //proof
}
else
cout << str1[i] << " is not alphabetic.\n";
i++;
}
return 0;
}
Last edited on
@pmwright

You had it pretty good, except for a few small items. I changed your char variable, to a string and initialized it. I added in what to search for as en ending for the while loop. But I think you should also let spaces be included in the new string, otherwise, everything runs together. But then, that may be what your after. So, anyway, here is a working copy of your program. I just have the string 'str_ofAlpha' be printed once at the end.

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
#include <iostream>
#include <string>
#include <cstdlib>
#include <stdio.h>
#include <ctype.h>

using namespace std;

int main()
{
	int i = 0;
	string str_ofAlpha = "";
	string str1;

	cout << "Enter text:\n\n";
	getline(cin, str1);

	while (str1[i] != '\0')
	{
		//This next code works to identify only the alphabetic characters:
		if (isalpha(str1[i]))
		{
			cout << str1[i] << " is alphabetic.\n"; //here is the proof.

			str_ofAlpha += str1[i];

			//cout << "str_ofAlpha now contains: " << str_ofAlpha << '\n'; //proof
		}
		else
			cout << str1[i] << " is not alphabetic.\n";
		i++;
	}
	cout << "The string 'str_ofAlpha' contains: " << str_ofAlpha << '\n'; //proof
	return 0;
}
Last edited on
@ pmwright

Here is a simple solution:
replace your line 25 with the following line of code:

size_t length = str1.copy(str_ofAlpha,i+1,0);

I changed the second parameter to i+1 and changed the third parameter to 0.
Thank you, whitenite1!

Your solution made it function exactly as I wanted with no spaces. =o)
My overall project takes user input, converts to all lowercase, strips white space and punctuation, then compares to a string array of valid commands which, if detected and validated, will trigger a response by the program, and the process will start over again and again as the users progress though my old fashioned text-based game.

Once again, THANK YOU!


Also, thank you, newbiee999. Since whitenite's solution worked, I didn't check yours yet. But I will. So, thanks in advance.
@pmwright

If your needing the inputted text to be converted to lower case, you would need to change one line.

Change this..
str_ofAlpha += str1[i];

to this..
str_ofAlpha += tolower(str1[i]);
Last edited on
That worked very well. Thank you again, whitenite1.
Topic archived. No new replies allowed.