String Manipulation Question

Jan 7, 2010 at 6:37pm
Hello,

First of all I'm really if i posted this here, when there is another topic about this. I can't seem to find it, and I'm really desperate to find the answer, or just a hint on how to do it.

Yesterday we were told to make a Program that will segregate/separate a single string that contains alphanumeric symbols. Then the values will be copied to a specific label.

Ex Output:

Enter a String: 1ILOVEYOU43

Numbers Only: 143
Letters Only: ILOVEYOU

I'm really just a beginner though I know how to manipulate a program once a get a hint on how to do it. We were also told to include the "duration" of the segregation.

Ex:

Enter a String: 1ILOVEYOU43

Numbers Only: 143 3s <----- Duration
Letters Only: ILOVEYOU 14s <----- Duration

I would really appreciate your help guys,I'm new here so I hope you can guide me..

Thank you so much..

P.S.

Im using TURBO C++ (I understand that the codes differ on editors and compilers).
Last edited on Jan 7, 2010 at 6:40pm
Jan 7, 2010 at 7:03pm
Well, start with something. We won't do the program for you, but we will help fix compile
errors and runtime bugs.

I have no idea what "duration" is or how it is computed given the above examples.
Jan 7, 2010 at 7:23pm
Well that's the problem I dont know where to start with the segregation I've only finished the second instruction which was to reverse a given string.

here is the 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.h>
#include <string.h>
#include <conio.h>

class String
{
	private:
		char ustr[30];


		char rev_str[30];
	public:
		void SetData(char str[30]) { strcpy(ustr, str);}
		void Reverse_string(void);

};
void String::Reverse_string(void)
{
	cout<<"\n\n\t\t\t";
	for(int i=strlen(ustr)-1; i >=0; i--)
	       {
			rev_str[i-(strlen(ustr)-1)]=ustr[i];
			cout<<rev_str[i-(strlen(ustr)-1)];
	       }
}



main()
{
	 String S;
	char aw[30];

	clrscr();

	cin>>aw;
	S.SetData(aw);
	S.Reverse_string();
	cout<<"\n\n Press any key to cont..";




	getch();


}




I've done some research, and I get results about String find, copy, erase and etc. but I cant seem to integrate it with the codes above, the codes provided in my searches are as follows:

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 <algorithm>
using namespace std;

int main () 
{
    string  str("C++ is best language");
    int pos1, pos2; // size_t or size_type 
                    // work not correct
    // search for first string "best" inside of str
    // default position is 0
    pos1 = str.find ("best");
    cout << "Word best is found on position " << pos1+1 
         << endl;
    
    // if pattern is not found - return -1
    pos2 = str.find ("best",pos1+1);
    cout << "Word best is found on position " << pos2+1 
         << endl;

    // search for first occurrence of character
    pos1 = str.find('g');
    cout << "First character 'g' found on position " 
         << pos1
    	 << endl;

    // search for first occurrence of string
    string s = "is";
    pos1 = str.find (s);
    cout << "Word 'is' is found on position " << pos1+1 
         << endl;
    
    return 0;
}


>.<
Jan 7, 2010 at 7:40pm
Walk the string, one character at a time, checking each character to see if it is a number
or a letter.
Jan 7, 2010 at 8:03pm
I've had the idea of using the str.find, lets say I'll let it find numbers then if its numbers it will print the numbers which it searched..

But how do I do that.. I mean I know how to use find but how to make the program print the one it found..
Jan 7, 2010 at 8:58pm
string::find returns the index into the string of the match, or string::npos if it did
not find a match. You can print the Nth character of a string like this:

cout << str[ N ];
Jan 9, 2010 at 4:45am
Keep it simple always!!!!!!!
u kow ASCII code 48 up to 57 are numbers!! , I wont do your homework. If working with string type, there's always a c_str() Function, think!!!!! u needa gather two diffrent character set and they got distinct values, make a choise... Trust it it's not that complicated!!!
Jan 9, 2010 at 5:04am
I dont understand the 'Duration' factor!....
Topic archived. No new replies allowed.