Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



How to input variable in string.find method in C++

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 29-01-2010
Member
 
Join Date: Dec 2009
Posts: 71
How to input variable in string.find method in C++

I have a c++ program in which i would like to input a variable in string.find as i want to use the same number as the index. Is it possible in C++ and also tell me how to achieve this. Also tell me whether i can set a variable in the find syntax and declare a variable and above all can the string.find() function will be able to read it. Please help me to sort this out.

Last edited by Caelaan : 29-01-2010 at 10:26 PM.
Reply With Quote
  #2  
Old 29-01-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Re: How to input variable in string.find method

You have not posted the code and hence i cannot give you a solution since it is not possible to imagine the whole program and post a code. It would be easier to implement on the problem if you post the code here and also try to explain the program since some of your points are not clear enough and also it does not present a clear picture to your problem.
Reply With Quote
  #3  
Old 29-01-2010
Member
 
Join Date: Dec 2009
Posts: 71
Re: How to input variable in string.find method in C++

Actually as i was in a hurry hence i could not post the program so i am posting the code here with this reply as you have asked some more information regarding this problem

int main ()
{
string s("aabbccddeeffgghhiijj") ;


string::size_type loc5 = s.find( "dde" , 0 ) ;


cout << loc1 << endl ;

If you see this program you will notice that the input here will be 7 since it continues to start from the starting of the string instead of putting 0 in the above statement can i declare anew variable and will i be able to declare a variable after this will i get that with the string.find() function.please help.

Last edited by Caelaan : 29-01-2010 at 10:41 PM.
Reply With Quote
  #4  
Old 29-01-2010
Member
 
Join Date: May 2008
Posts: 1,990
Re: How to input variable in string.find method in C++

I think you should try this by declaring the index as an int. As far as i know i think that the code should work for sure and hence i have got a solution which should work in your case try the code below

int main ()
{
string s("aabbccddeeffgghhiijj") ;
int pos1 = 5;

string::size_type loc2 = s.find( "dde" , pos1 ) ;
cout << loc1 << endl ;

Last edited by Katty : 29-01-2010 at 10:50 PM.
Reply With Quote
  #5  
Old 29-01-2010
Member
 
Join Date: Dec 2009
Posts: 71
Re: How to input variable in string.find method in C++

Thanks for the code i have tried the code but it seems that it is not working because i was of the opinion that when i specify the index where to start to search then i will get the result from where it starts off . Consider for example if the index is 5 it will start reading from the alphabet e but the output i get is still from 20.
Reply With Quote
  #6  
Old 29-01-2010
Member
 
Join Date: May 2008
Posts: 1,990
Re: How to input variable in string.find method in C++

I thought of a different approach by which i think that you will get this solved if you add on the value in which the position started you might get the solution please consider the code which i have mentioned below:

#include <iostream>
#include <string>

int main()
{
std::string str("aabbccddeeffgg");
size_t pos = 0;


pos1 = str.find('n', 1);
std::cout << pos1 << '\n\n';


pos1 = str.find('n', 0);// start from 0
std::cout << pos1;

std::cin.get();
return 0;
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How to input variable in string.find method in C++"
Thread Thread Starter Forum Replies Last Post
Assign a text file from hard disk into a VBA string variable AbiCgail Software Development 5 15-07-2011 11:49 AM
How to Convert String to Variable Name in C#? PsYcHo 1 Software Development 6 10-04-2010 01:29 AM
How to get only digits from an input string? Luz Software Development 4 09-02-2010 08:42 PM
Variable method arguments in Csharp KAMANA Software Development 3 14-11-2009 05:57 PM
Problem with variable input in C program Zool Software Development 2 14-05-2009 10:43 PM


All times are GMT +5.5. The time now is 05:17 AM.