LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-18-2010, 04:14 PM   #1
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Rep: Reputation: 16
substring implementation for String


Code:
#include <iostream>
#include <string>
using namespace std;

class My_String {
	string s;
public:
	My_String(string ss): s(ss) {}
	string substr(int pos = 0, int n = string::npos)
	{
		string str;
		int j = 0;
		for(int i = pos; i <= n; i++)
		{
			str[j] = s[i];
			j++;
		}
		return str;
	}
};

int main()
{
	My_String test("i wanna be a milionar");
	string obj = test.substr(3,6);
	cout << obj;
}
My code doesnt print nothing,any idee why ? Thank you
 
Old 10-18-2010, 04:25 PM   #2
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
Code:
#include <iostream>
#include <string>
using namespace std;

class My_String {
	string s;
public:
	My_String(string ss): s(ss) {}
	string substr(int pos = 0, int n = string::npos)
	{
		string str;
		int j = 0;
		for(int i = pos; i <= n; i++)
		{
			str[j] = s[i];
			j++;
		}
		return str;
	}
};

int main()
{
	My_String test("i wanna be a milionar");
	string obj = test.substr(3,6);
	cout << obj;
}
My code doesnt print nothing,any idee why ? Thank you
What did you do to debug your code ? How about inserting diagnostic prints and checking validity of you assumptions step by step - this is what debugging is about.
 
Old 10-18-2010, 04:27 PM   #3
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
I debug it,dont asume i dont,it works great in function,but it doesnt return,it skips return str :|
 
Old 10-18-2010, 04:31 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
I debug it,dont asume i dont,it works great in function,but it doesnt return,it skips return str :|
How do you know "it skips return str" ? And why do you think your 'return' statement is correct in the first place ?

Here is a much simpler code for you:

Code:
int *foo()
  {
  int i = 2;
  int *ptr = &i;
  return ptr;
  }
- is this code correct ?
 
Old 10-18-2010, 04:36 PM   #5
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
yes
 
Old 10-18-2010, 04:39 PM   #6
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
If i debug it,i step trough code,so i can see it skips return str;
 
Old 10-18-2010, 04:44 PM   #7
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
yes
If you think the code I posted is correct, you are wrong. Think which kind of storage (static/global, stack, heap) 'i' variable is located in.
 
Old 10-18-2010, 04:44 PM   #8
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
When it reaches end of function -> } str = anna\25 ...thats what teh debugger says
 
Old 10-18-2010, 04:45 PM   #9
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
its stack....i was refering corect that it is compiling,i dont know what you are trying to do,if you try to return the value,its wrong
 
Old 10-18-2010, 04:47 PM   #10
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
its stack....i was refering corect that it is compiling,i dont know what you are trying to do,if you try to return the value,its wrong
So, where is your 'str' located ?
 
Old 10-18-2010, 04:52 PM   #11
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
It doesnt have to be local ?
 
Old 10-18-2010, 04:54 PM   #12
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
It doesnt have to be local ?
I think it's on stack. I.e. I think you are trying to return a pointer to something whose contents are on stack - exactly the same way as in my intentionally wrong example.
 
Old 10-18-2010, 04:59 PM   #13
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
And a solution ?
 
Old 10-18-2010, 05:01 PM   #14
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by vbx_wx View Post
And a solution ?
'new'.
 
Old 10-18-2010, 05:03 PM   #15
vbx_wx
Member
 
Registered: Feb 2010
Posts: 181

Original Poster
Rep: Reputation: 16
i tried alocating on the heap,still nothing:

Code:
	string substr(int pos = 0, int n = string::npos)
	{
		string* str = new string;
		int j = 0;
		for(int i = pos; i <= n; i++)
		{
			str[j] = s[i];
			j++;
		}
		return *str;
	}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash: test for substring inside string ali_bush Linux - General 3 04-26-2018 07:31 PM
i need to find a substring in a string and get the next 3 characters anurupr Linux - Newbie 8 03-07-2010 05:45 PM
extract substring from string in C baddah Programming 6 02-02-2010 04:22 AM
replace a substring with another string in C zeppelin Programming 21 11-09-2009 09:59 PM
how many occurances of a substring are they n a string linuxmandrake Programming 1 04-11-2006 02:02 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:37 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration