?
Solved

Substring

Posted on 1999-12-07
14
Medium Priority
?
894 Views
Last Modified: 2008-02-01
Can anyone tell me what is syntax of (mid(theString, 1, 1)) in C/C++ ? (In console)
Another question, can anyone tell me where should I go (website) if I am the beginner of visual C++ (windows programming, just like VB).
0
Comment
Question by:ahchong
14 Comments
 

Author Comment

by:ahchong
ID: 2264016
Edited text of question.
0
 
LVL 7

Expert Comment

by:Jan Louwerens
ID: 2264038
if you are are using the string class, use:
substr(start, length)  // use 'string::npos' for length to go till end of string

string a = "hello world";
string b = a.substr(3, 5); // b = 'lo wo'
0
 
LVL 3

Expert Comment

by:akalmani
ID: 2264049
Hi !!
   I got the answer for CString mid but u wanted only the mid function i am not aware.

For VC++ there is a book called ->
1)No experience required
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
LVL 7

Expert Comment

by:Jan Louwerens
ID: 2264064
CString is a Microsoft MFC class, and is not a C/C++ standard class. string is a standard C++ class.
If you're using MFC, the CString class may make things easier for you. If not, the string class is your best choice.

if you are using standard null terminated strings (char *), then use the strncpy function:

char* strncpy(char* dest, char* src, length n)
0
 
LVL 3

Accepted Solution

by:
V_Bapat earned 20 total points
ID: 2264178
You should ask only one question at a time as it makes it easy for answering as well as for you to accept the answer and grade the answer.

> Can anyone tell me what is syntax of (mid(theString, 1, 1)) in C/C++ ? (In console)
I think you have got answer from jlouwere.

> Another question, can anyone tell me where should I go (website) if I am the beginner of visual C++ (windows programming, just like VB).
The best site I can tell you is:
http://devcentral.iftech.com/learning/tutorials
where you will find everything: C, C++, VC++/MFC, VB, Java to name a few.

Vicky
0
 

Author Comment

by:ahchong
ID: 2264516
jlouwere:
   string a = "hello world";
   string b = a.substr(3, 5); // b = 'lo wo'
thank your for your answer, but I can't use it...
I am using Borland c/c++ 3.0, but I still don't know how to use the substr... seem that my c/c++ 3.0 have no this function... (when I searched for my help file, it seem to be no this function at all ), then how can I do it (or could you write a complete script) ????
all your response will be appreciated, thanks !
0
 

Author Comment

by:ahchong
ID: 2264536
jlouwere:
   string a = "hello world";
   string b = a.substr(3, 5); // b = 'lo wo'
thank your for your answer, but I can't use it...
I am using Borland c/c++ 3.0, but I still don't know how to use the substr... seem that my c/c++ 3.0 have no this function... (when I searched for my help file, it seem to be no this function at all ), then how can I do it (or could you write a complete script) ????
all your response will be appreciated, thanks !
0
 
LVL 7

Expert Comment

by:Jan Louwerens
ID: 2266094
the substr(...) function is a method of the class 'string'. it is not a global c function. To use the string class, make sure that you include the line:

#include <string>

at the top of your source file. This will allow you to use the string class

(How old is Borland 3.0? Does it contain the STL classes? string is one of the STL classes)
0
 

Author Comment

by:ahchong
ID: 2267667
By Using Visual C++ 5.0:
#include <iostream.h>
#include <string.h>
void main (void) {  
      cout << "Hello world".substr(3,5);      
}
This is the problem:
error C2228: left of '.substr' must have class/struct/union type
   How can I correct it ?
0
 
LVL 7

Expert Comment

by:Jan Louwerens
ID: 2269600
#include <iostream>
#include <string>

void main (void) {    
   string msg = "Hello world";
   cout << msg.substr(3,5);
}
0
 

Author Comment

by:ahchong
ID: 2270622
sorry that I am the beginner of C language...
jlouwere :
from your source code..
#include <iostream>
#include <string>

void main (void) {    
   string msg = "Hello world";
   cout << msg.substr(3,5);
}

I can't run it ... it said the "string" got problem... how can I fix it ?
0
 
LVL 7

Expert Comment

by:Jan Louwerens
ID: 2270645
what do you mean by "string got problem"?
can you print exactly what the error is?

Also, are you using C++ or only straight C? string is a class defined in the STL (Standard Template Library) for the C++ language.
0
 

Author Comment

by:ahchong
ID: 2270682
Now I am using vc++,
#include <iostream>
#include <string>

void main (void) {      
   "string" msg = "Hello world";
   cout^ << msg.substr(3,5);
}      |
      this string got problem....
0
 
LVL 7

Expert Comment

by:Jan Louwerens
ID: 2272317
maybe try this for that line:

std::string msg = "Hello world";

DO you have the online docs that come with VC++? I think Mocrosoft has their own implementation of the STL, and that may be causing some kind of conflict.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

589 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question