| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
All About Strings.
I'm having problem because I don't know how could I code my program.
Example: Sentence: "You and I are perfect!" How can I manage to get the line "I" to "perfect!"? Using: Start String = I End String = ct! That would result to: I are perfect! The concept is that I would specify the start of string and the end of the string like a keyword but the result would be the whole line from I to perfect? |
|
#2
|
|||
|
|||
|
If you're using c++ strings, it is really easy.
You just need the find() function. Some simple code: Code:
#include <iostream>
#include <string>
using namespace std;
string fstringfinder(string start, string end, string search)
{
string return_value;
int start_loc = (int) search.find(start,0); //find the location of the starting string
int end_loc = (int) search.find(end,0); //find location of ending string
int len_substr = end_loc - start_loc + 1; //the length of the new string
if ((end_loc < start_loc) || (start_loc < 0) || (end_loc < 0))
//error - either the starting string is after the ending one, or one
//of the 2 strings is not found in the searched string
return "Error!"; //good idea to be substituted with something more practical, like a list of error codes.
else
return_value= search.substr(start_loc,len_substr);
return return_value;
}
int main()
{
string a("0123456789");
cout << fstringfinder("4","7",a);
return 0;
If you are using bare c it's a little more complicated, post again and i'll try to explain to you Hope that helps ![]() |
|
#3
|
|||
|
|||
|
no not pure C i go for C++ but too bad i cannot understand much though.. but thanks for your post
If you could contruct me a prog that opens text files in the current dir and searches the given start and endstrings and then compiling them into one txt file somewhat string isolation... BTW thanks again ![]() |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > All About Strings. |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|