473,396 Members | 1,713 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

C++ String Terminator

46
Hi, : )
I know that C++ Strings are not terminated with a NULL terminator like C strings, so what are they terminated with? I'm making a calculator currently that parses an expression by separating the numbers and operators and putting them in a list with accordance to the order of operations. When I parse the expression, I go through the string with a for loop, and test each character against a switch statement. If the character is a number (0-9), then its added to a separate string, when the switch reaches an operator, it dumps the separate string into the list and starts over. The problem is getting the last number, because there is no operator after it. Think "22+33", there is nothing after 33 to test the switch against, and thus, the string never gets dumped into the list. Any way I can test for the end of the string, or anyone got a different idea to go about this whole process?

Thanks,
Austen
Apr 20 '07 #1
12 14954
JosAH
11,448 Expert 8TB
Hi, : )
I know that C++ Strings are not terminated with a NULL terminator like C strings, so what are they terminated with? I'm making a calculator currently that parses an expression by separating the numbers and operators and putting them in a list with accordance to the order of operations. When I parse the expression, I go through the string with a for loop, and test each character against a switch statement. If the character is a number (0-9), then its added to a separate string, when the switch reaches an operator, it dumps the separate string into the list and starts over. The problem is getting the last number, because there is no operator after it. Think "22+33", there is nothing after 33 to test the switch against, and thus, the string never gets dumped into the list. Any way I can test for the end of the string, or anyone got a different idea to go about this whole process?

Thanks,
Austen
If you need a '\0' character at the end of the string you can even mimic it:
Expand|Select|Wrap|Line Numbers
  1. for (int i= 0, n= your_string.size(); i <= n; i++) {
  2.    char chr= (i == n)?'\0':your_string[i];
  3.    // your code here 
  4. }
You can even append a literal '\0' char to your_string and remove it again when
you're done with parsing the string, but I prefer the approach above.

kind regards,

Jos
Apr 20 '07 #2
ayan4u
86
even with c++ strings thr is a null termination....cause u can iterate thru the string makng a condition tht u continue unless u meet the NULL...

i.e.,
Expand|Select|Wrap|Line Numbers
  1. string expression ="Am i worthy enough":
  2. int i=0;
  3. while(expression[i]!=NULL)
  4. { code...
  5. i++;}
  6.  
Apr 20 '07 #3
JosAH
11,448 Expert 8TB
even with c++ strings thr is a null termination....cause u can iterate thru the string makng a condition tht u continue unless u meet the NULL...
That is not true; try the at() member function instead of operator[] and see for
yourself. (at() performs a range check, operator[] simply goes berzerk).

kind regards,

Jos
Apr 20 '07 #4
sake
46
That is not true; try the at() member function instead of operator[] and see for
yourself. (at() performs a range check, operator[] simply goes berzerk).

kind regards,

Jos
Yeah, I've tried testing for a NULL terminator, but it didn't work. I guess this has to do with not having to define the space required for a string. I think the easiest way to get this done is to just manually append a '\0' to the end of the string.

Thanks Josah and Ayan : )
Apr 20 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
You test for end of a C++ string by using an iterator:

string::iterator itr;

itr = str.begin();

while (itr != str.end())
{
//string hasn't ended
++itr;
}
//string has ended.

STL uses iterators everywhere.

The null terminator is for a C string. Try not to make C++ into C.
Apr 20 '07 #6
ayan4u
86
Yeah, I've tried testing for a NULL terminator, but it didn't work. I guess this has to do with not having to define the space required for a string. I think the easiest way to get this done is to just manually append a '\0' to the end of the string.

Thanks Josah and Ayan : )
u are saying tht running a loop around the string object till u encounter /0 isn't operating...i know the operator [] is sumthing bizzare with c++ strings...but the above said process does work...and i checkd....

u can as well use iterators....they r friendly....but i just responded as i saw in one f my pros tht '\0' chkn in strings is workn....
Apr 20 '07 #7
JosAH
11,448 Expert 8TB
u are saying tht running a loop around the string object till u encounter /0 isn't operating...i know the operator [] is sumthing bizzare with c++ strings...but the above said process does work...and i checkd....

u can as well use iterators....they r friendly....but i just responded as i saw in one f my pros tht '\0' chkn in strings is workn....
There is nothing bezarre about the operator[], it is simply overloaded. There
is no '\0' character following the other characters in a string. Read chapter 20,
page 586 of Stroustrup's "The C++ Programming Language, edition III or the
documentation of any conforming STL. If there *does* happen to be a trailing
'\0' character at position string.size() that'd be just serendipity, i.e. undefined.

kind regards,

Jos
Apr 20 '07 #8
weaknessforcats
9,208 Expert Mod 8TB
If you insist on using a null terminator check like the string objkect is a C string then call the method that makes the string object appear as a const C string:

Expand|Select|Wrap|Line Numbers
  1. if (str.c_str()[i] == '\0')
  2. {
  3.     //string has ended
  4. }
  5.  
Apr 20 '07 #9
JosAH
11,448 Expert 8TB
If you insist on using a null terminator check like the string objkect is a C string then call the method that makes the string object appear as a const C string:

Expand|Select|Wrap|Line Numbers
  1. if (str.c_str()[i] == '\0')
  2. {
  3.     //string has ended
  4. }
  5.  
If you'd put that in a loop it would be extremely inefficient, i.e. the c_str()
function cannot assume that the string data had remained the same between
two c_str() invocations so it would allocate another buffer again for a copy
of the relevant string data.

kind regards,

Jos
Apr 20 '07 #10
sake
46
I'm not insisting to use a Null terminator, I'm just trying to find the best way. The iterator seems to work. Thanks alot for everyone's help :-)
Apr 20 '07 #11
sake
46
Sorry about double posting, but I have just one more question. I didn't think it needed a new thread.
I've tried using the clear() method, but it doesnt seem to get rid of verything in a string. I shtere any other way to surely eradicate everything in a string? Like make all of the characters it contains blank. I've also tried doing that manually within a for loop, setting every element to: '' (two single quotes), "" (two double quotes), and NULL. None of these worked, however. Specifically, my problem is when I convert my string to a C string and then convert that into a number. Because the string storing the number is reused, a larger number preceding the new number will not get overwritten (Think "235+3", when parsed, "3" will become "335". Or, "3"+"35", because only the "2" in the previous string was overwritten. Any ideas?
Thanks again,
Austen
Apr 20 '07 #12
ayan4u
86
wht dnt u use erase....

string s ="abcd is 1234";

s.erase(4,2);

this will make s = "abcds 1234";

s.erase(2);

this will make s="ab";

s.erase(); // this erases the entire string

or u can use iterators...

s.erase(iterator begin, iterator end);

s.erase(iterator pos);


now as far as detecting charecters as numbers...

use isdigit() or isalpha() included in ctype.h....then use the corresponding ASCII value if you find it to be a digit...
Apr 21 '07 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: John Smith | last post by:
Hi, I'm writing a library in C++ which is supposed to be used by people using C. One function I have must return a string to users which is arbitrary length. The user must be able to use this...
3
by: huey_jiang | last post by:
Hi All, I am trying to figure out a right syntax to convert an integer array into hex array. sprintf worked for me on doing single integer: int i, Iarray, n=15; char buf; sprintf(buf,...
4
by: Locusta | last post by:
Hello, I have been struggeling for replacing a string in a string. The snippet from the program below replaces the <, & and > with the XML equivalent values. In the program, I allocate space...
2
by: Edward | last post by:
My function to populate fields on a form from a collection: Private Sub cbfSetUpAddresses(ByVal pcolAddress As Collection) Line 1 Me.txtAddress1.Text = pcolAddress(1).ToString Line 2 ...
10
by: tommaso.gastaldi | last post by:
I am extracting some field names from a table of a db (it's an OledbSchemaguid table). It occurs for some OleDbSchema driver that NameDBField = cstr(DataRow.Item(3)) return a string that is...
19
by: Adam | last post by:
Hi, I'd like to return an (arbitrary length) string array from a function so that after calling the array I've got a list of strings I can access. After much perusing of the internet I found a...
5
by: TBass | last post by:
Hi, I'm moving a socket library I wrote from C to C++. In the C version, I had to malloc char arrays to store incoming communication. My hope was to use std::string in C++, but then I realized a...
1
by: JKAT | last post by:
Hi All, I am using a com object to maniplate data inside an external program using VB.NET. One of the methods this com object provides requires 1 string parameter. The external program,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.