Find character within string

I have a word from a parts of speech wordlist that looks like this:

aardvark\N

I would like to write code to separate the "\N" so that the program will first find aardvark and then return "\N". The program is supposed to return the "Parts of Speech" for any word that you enter...

how would I iterate through the word "aardvark" and stop at the "\N"?
Iterate through until the character you're at is a backslash.
Are you allowed to use standard functions? Classes?

In another of your mails you're using std::getline with a char buffer. It would be better to use std::getline with a std::string. Then you could use something along the lines of:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void Test()
{
    string s = "aardvark\\N";

    string::size_type pos = s.find('\\');

    if(s.npos != pos)
    {
        string word = s.substr(0, pos);
        string partOfSpeech = s.substr(pos + 1);
   
        cout << "word = " << word << endl;
        cout << "partOfSpeech = " << partOfSpeech << endl;
    }
}


If you prefer to use char buffers, you can use strchr() and strncpy() to achieve the same thing.
Last edited on
compiler issues error: "missing terminating character."
new problem...the buffer in the parts of speech word list is a forward slash..."\" . Here is code so far:
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
int main()
{
	string line, line3, line4;
	char line2[6] ;
	strcpy(line2, "life");
	char* cstr;
	char* word_ptr;
	char* pos_ptr;
	
	ifstream myfile ("/Users/appliedstatistics/Language/mobypos.txt");
	//ofstream newfile ("/Users/appliedstatistics/Language/newdict.txt");
	if(myfile.is_open()){
		do{
			getline(myfile, line);
			//newfile << "\0" << "\b" << line << "\n";
			//const int length = line.length();
			cstr = new char [line.size()+1];
			strcpy (cstr, line.c_str());

			
			word_ptr = cstr;
			pos_ptr = strchr(cstr, '\' );
			*pos_ptr = '\0';
			++pos_ptr;
			
			cout << word_ptr << " - " << pos_ptr << endl;
			
			//if(!strcmp(cstr, line2)){cout << cstr << endl;}
		}while(myfile.good()); 
	}	
		myfile.close();
	
	
		
	//else cout << "Unable to open file"; 			
	
	return 0;
} 


Can I get around the use of this buffer or am I going to need another new word list?
Last edited on
for whatever reason, I added "\\" into the following code and the program now outputs the word and part of speech separately...everything works but I would appreciate an explanation if anyone has one...is "\\" a way of pointing towards a '\' in a 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
39
40
41
42
43
44
45
46
47
48
int main()
{
	string line, line3, line4;
	char line2[6] ;
	strcpy(line2, "life");
	char* cstr;
	char* word_ptr;
	char* pos_ptr;
	
	ifstream myfile ("/Users/appliedstatistics/Language/mobypos.txt");
	//ofstream newfile ("/Users/appliedstatistics/Language/newdict.txt");
	if(myfile.is_open()){
		do{
			getline(myfile, line);
			//newfile << "\0" << "\b" << line << "\n";
			//const int length = line.length();
			string::size_type pos = line.find("\\");
			
			if(line.npos != pos)
			{
				string word = line.substr(0, pos);
				string partOfSpeech = line.substr(pos + 1);
				
				cout << "word = " << word << endl;
				cout << "partOfSpeech = " << partOfSpeech << endl;
			}
		
			
			
			//word_ptr = line5;
			//pos_ptr = strchr(cstr, '\ ' );
			//*pos_ptr = ' ';
			//++pos_ptr;
			
			//cout << word_ptr << " - " << pos_ptr << endl;
			//strcpy (cstr, line.c_str());
			//cstr = new char [line.size()+1];
			//if(!strcmp(cstr, line2)){cout << cstr << endl;}
		}while(myfile.good()); 
	}	
		myfile.close();
	
	
		
	//else cout << "Unable to open file"; 			
	
	return 0;
}
Last edited on
\ is a backslash and, by itself, is used to escape chars in C++.

\n is a newline
\\n is a backslash character followed by a lowercase n
much thanks...
Topic archived. No new replies allowed.