For a project i need to find strings in double quotes and output them to screen, My problem is it finds the first double quote in '"', causing it all to go to hell from there and giving me this output
1 2 3 4 5 6 7 8 9 10 11 12 13 14
"' )
break;
}
//store characters untill the end is found
string buffer;
buffer += ch;
while(in.get( ch ) )
{
if( ch == '""stringfinder.cpp""output.txt"
I can't think of how to get rid of any '"' in the .cpp file before searching for strings. Any ideas plz?
Heh, this was fun. I went and wrote myself a little program to dump all the string literals (with their starting line numbers) found in a C or C++ source file.
There are three things you need to watch out for:
1) commentary.
/* this is "a comment" */
"but /* this */ is not "
2) implicitly quoted characters
'"'
3) explicitly quoted characters
\"
This category also includes the infamous
"this string begins on one line, \
but ends on the next"
I handled this by converting the quoted-newline literal into a newline escape:
"this string begins on one line, \nbut ends on the next"
There is also the case of string literal concatenation
"this string begins on one line, "
"but ends on the next"
My program treats them as distinct string literals even though the compiler will combine them into a single string constant, since they are distinct literals in the source code. I think this is the correct choice, but you may disagree. How you handle it is up to you.
[edit] Oh, you must also handle the case where the source file contains errors -- and complain if you hit the end of the file before terminating a string constant.
Ya this is homework. But anyway i figured it out, but have another problem, lets say there is a string "Say \"hello\" to everyone" how do i find the backslash, cuz '\\' doesn't seems to be working for me.