473,855 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

substring

Hey,
I would like to know 2 things.
1)Is there any function (in C standard library) that extracts a
substring from a string?
2)Is there any function (in C standard library) that returns the
position of a substring in a string?

Thx a lot...
Nov 13 '05
62 31948
Dan Pop wrote:
An implementation doing array bounds checking *can* detect that the end
of the array has been reached without encountering any null character.
At this point, the implementation is free to do anything it wants,
including making demons fly out of your nose.


I find this a bit upsetting, if true. This means that we can have two
pointers that compare equal, one of which is known to point to a valid
object, and yet dereferencing the other has undefined behaviour. For
example, in the following, loop 2 has (according to the above)
undefined behaviour, while loop 3 does not.

char s1[3] = "123";
char s2[4] = "456";

if (s2 == s1 + sizeof s1) {
char *p = s1, *q = s2;

/* loop 1 */
for (; p != q; p++) {
putchar(*p);
}

assert (p == q);

/* loop 2 */
for (; p != s2 + sizeof s2; p++) {
putchar(*p);
}

/* loop 3 */
for (; q != s2 + sizeof s2; q++) {
putchar(*q);
}
}

Jeremy.
Nov 13 '05 #41
On Wed, 05 Nov 2003 18:58:44 +0000, Thomas Stegen <ts*****@cis.st rath.ac.uk>
wrote:
rihad wrote:
On Wed, 05 Nov 2003 14:47:52 GMT, pete <pf*****@mindsp ring.com> wrote:
Given this:

char s[] = "123456", (*p3)[3] = &s;
Incompatible pointer types. &s is of type (*)[6].


It's actually of type (char (*)[7]), but nontheless I hoped it would be a valid
assignment. Alas... Would be neat though if it were :)

is calling

printf("%s\n", p3[0]);
Illegal, %s expects a pointer to char not a pointer to
char[3]


p3[0] is an expression of type (char [3]) which decays into (char *).

illegal, but

printf("%s\n", p3[1]);
Same here.


Same here.
puts and printf are different because puts prints a string, while
printf explicitly takes a null terminated array.


Gosh! What is the difference between a string and a zero-terminated array (of
chars)?! Please help the desperate!

Nov 13 '05 #42
rihad wrote:

On Wed, 05 Nov 2003 14:47:52 GMT, pete <pf*****@mindsp ring.com> wrote:
rihad wrote:
&s1[0] points to an array of objects.
The array is ended by a ((char) 0).


The array is terminated by a ((char)'3')
char s1[3] = "123";


The array of objects terminated by a ((char) 0), not s1.
char s1[3] = "123";
char s2[4] = "456";


s1 and s2 are two distinct arrays.
s2, is not part of s1.
s2 ends in a null character.
s1 ends in '3'.

--
pete
Nov 13 '05 #43
On 5 Nov 2003 19:39:58 GMT, Jeremy Yallop <je****@jdyallo p.freeserve.co. uk>
wrote:
Dan Pop wrote:
An implementation doing array bounds checking *can* detect that the end
of the array has been reached without encountering any null character.
At this point, the implementation is free to do anything it wants,
including making demons fly out of your nose.


I find this a bit upsetting, if true. This means that we can have two
pointers that compare equal, one of which is known to point to a valid
object, and yet dereferencing the other has undefined behaviour. For
example, in the following, loop 2 has (according to the above)
undefined behaviour, while loop 3 does not.

char s1[3] = "123";
char s2[4] = "456";

if (s2 == s1 + sizeof s1) {
char *p = s1, *q = s2;

/* loop 1 */
for (; p != q; p++) {
putchar(*p);
}

assert (p == q);

/* loop 2 */
for (; p != s2 + sizeof s2; p++) {
putchar(*p);
}

/* loop 3 */
for (; q != s2 + sizeof s2; q++) {
putchar(*q);
}
}


If loop 2 is undefined, there's no point in living. Thanks for the eye-opening
example, Jeremy.

Nov 13 '05 #44
On 5 Nov 2003 19:28:45 GMT, Jeremy Yallop <je****@jdyallo p.freeserve.co. uk>
wrote:
Thomas Stegen wrote:
puts and printf are different because puts prints a string, while
printf explicitly takes a null terminated array.


I don't believe this is true. Consider the following text from C99
7.1.4 ("Use of library functions"):

If a function argument is described as being an array, the pointer
actually passed to the function shall have a value such that all
address computations and accesses to objects (that would be valid if
the pointer did point to the first element of such an array) are in
fact valid.

In the library section of the standard the word "array" is just a
convenient shorthand to denote array-like objects (including the
object returned from malloc(), for example). You can't draw any
conclusions from the fact that the description of fprintf() uses the
word "array" to describe the pointer-to-string passed as argument and
the description of puts() doesn't.


That's what I've felt since my first followup to Dan Pop! Maybe I haven't been
thinking in terms of the standard's wording but nontheless I'm glad to see that
you happen to share my opinion, even though yours is far more educated, while
mine is based on what "makes sense to me" :)
Nov 13 '05 #45

"Dan Pop" <Da*****@cern.c h> schrieb im Newsbeitrag
news:bo******** **@sunnews.cern .ch...
In <3f************ ***********@new sreader02.highw ay.telekom.at> "Robert Stankowic" <pc******@netwa y.at> writes:
Do you know any possibility for an implementation to produce different
results (provided the expression "s2 == s1 + sizeof s1" yields true)?


Of course. Any implementation doing array bound checking *properly*
should object in the s1/s2 case. The big challenge of such an
implementation is NOT to object to the puts call.


Thank you for the clarification
regards
Robert
Nov 13 '05 #46
In <sl************ *******@ekoi.cl .cam.ac.uk> Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> writes:
Thomas Stegen wrote:
puts and printf are different because puts prints a string, while
printf explicitly takes a null terminated array.
I don't believe this is true. Consider the following text from C99
7.1.4 ("Use of library functions"):

If a function argument is described as being an array, the pointer
actually passed to the function shall have a value such that all
address computations and accesses to objects (that would be valid if
the pointer did point to the first element of such an array) are in
fact valid.

In the library section of the standard the word "array" is just a
convenient shorthand to denote array-like objects (including the
object returned from malloc(), for example).


The word "array" being defined by the standard, cannot be interpreted in
any other way when used in the standard. The object returned by malloc
satisfies the standard's definition of array.
You can't draw any
conclusions from the fact that the description of fprintf() uses the
word "array" to describe the pointer-to-string passed as argument and
the description of puts() doesn't.


Of course you can. If you ignore the definitions of the terms used by
the standard in a purely arbitrary way (i.e. according to your own
preconceptions about the language), the standard becomes a useless
document.

The *real* issue is whether the current wording of the standard accurately
reflects the intent of those who wrote it. If it doesn't, the wording
needs to be fixed, but until then, one cannot take arbitrary liberties
in interpreting the text of the standard.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #47
In <sl************ *******@ekoi.cl .cam.ac.uk> Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> writes:
Dan Pop wrote:
An implementation doing array bounds checking *can* detect that the end
of the array has been reached without encountering any null character.
At this point, the implementation is free to do anything it wants,
including making demons fly out of your nose.
I find this a bit upsetting, if true. This means that we can have two
pointers that compare equal, one of which is known to point to a valid
object, and yet dereferencing the other has undefined behaviour.


Yup, C99 *explicitly* mentions this possibility:

6 Two pointers compare equal if and only if both are null pointers,
both are pointers to the same object (including a pointer to an
object and a subobject at its beginning) or function, both are
pointers to one past the last element of the same array object,
or one is a pointer to one past the end of one array object and
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
the other is a pointer to the start of a different array object
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
that happens to immediately follow the first array object in
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^
the address space.91)
^^^^^^^^^^^^^^^ ^^
_______________ _____

91) Two objects may be adjacent in memory because they are
adjacent elements of a larger array or adjacent members of
a structure with no padding between them, or because the
implementation chose to place them so, even though they
are unrelated. If prior invalid pointer operations (such as
accesses outside array bounds) produced undefined behavior,
subsequent comparisons also produce undefined behavior.
For
example, in the following, loop 2 has (according to the above)
undefined behaviour, while loop 3 does not.

char s1[3] = "123";
char s2[4] = "456";

if (s2 == s1 + sizeof s1) {
char *p = s1, *q = s2;
Stylistic issue: the code is much more readable if you name the pointers
p1 and p2, to be consistent with the way they are initialised.
/* loop 1 */
for (; p != q; p++) {
putchar(*p);
}

assert (p == q);
What for?!? Don't you trust the compiler to get the exit condition from
loop1 right or do you suspect that both != and == can evaluate to false
on the same pointer operands?
/* loop 2 */
for (; p != s2 + sizeof s2; p++) {
putchar(*p);
}
You can increment p one past the end of its object, but the
result cannot be either dereferenced or further incremented.

8 When an expression that has integer type is added to or subtracted
from a pointer, the result has the type of the pointer operand. If
the pointer operand points to an element of an array object,
and the array is large enough, the result points to an element
offset from the original element such that the difference of the
subscripts of the resulting and original array elements equals
the integer expression. In other words, if the expression P
points to the i-th element of an array object, the expressions
(P)+N (equivalently, N+(P)) and (P)-N (where N has the value n)
point to, respectively, the i+n-th and i-n-th elements of the
array object, provided they exist. Moreover, if the expression
P points to the last element of an array object, the expression
(P)+1 points one past the last element of the array object, and
if the expression Q points one past the last element of an array
object, the expression (Q)-1 points to the last element of the
array object. If both the pointer operand and the result point to
elements of the same array object, or one past the last element of
the array object, the evaluation shall not produce an overflow;
otherwise, the behavior is undefined. If the result points one
^^^^^^^^^^^^^^^ ^^^^^^^^^
past the last element of the array object, it shall not be used
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^
as the operand of a unary * operator that is evaluated.
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^

There is no possible doubt that, according to the standard, your code
invokes undefined behaviour.
/* loop 3 */
for (; q != s2 + sizeof s2; q++) {
putchar(*q);
}
}


No problems here.

Imagine that you were writing a bounds checking implementation. It is
obvious, from these quotes, that pointer equality checking would have to
ignore the bounds information, but the indirection operator would have to
take it into account, as well as the addition and subtraction operators.

If your implementation would silently execute loop2, it would fail to
report a bound violation related invocation of undefined behaviour.

This is a typical example of how a very common mental image of the C
language is at odds with the C standard. Most people would expect loop2
to work and it will work on most (if not all) implementations without
bounds checking, but it will work by accident, not by design.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #48
Dan Pop wrote:
In <sl************ *******@ekoi.cl .cam.ac.uk> Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> writes:
Thomas Stegen wrote:
puts and printf are different because puts prints a string, while
printf explicitly takes a null terminated array.


I don't believe this is true. Consider the following text from C99
7.1.4 ("Use of library functions"):

If a function argument is described as being an array, the pointer
actually passed to the function shall have a value such that all
address computations and accesses to objects (that would be valid if
the pointer did point to the first element of such an array) are in
fact valid.

In the library section of the standard the word "array" is just a
convenient shorthand to denote array-like objects (including the
object returned from malloc(), for example).


The word "array" being defined by the standard, cannot be interpreted in
any other way when used in the standard.


I'm not sure why you say that. The section that I quoted clearly
states that "array" has a broader sense when used in the library
section.
You can't draw any
conclusions from the fact that the description of fprintf() uses the
word "array" to describe the pointer-to-string passed as argument and
the description of puts() doesn't.


Of course you can. If you ignore the definitions of the terms used by
the standard in a purely arbitrary way (i.e. according to your own
preconceptions about the language), the standard becomes a useless
document.


There's nothing arbitrary about making use of the explicit exception
given in the introduction to the library section of the standard
(quoted above). The word "array" is used in the library section for a
data pointer on which certain operations are valid. Here's another
example:

size_t fread(void * restrict ptr,
size_t size, size_t nmemb,
FILE * restrict stream);

The fread function reads, into the array pointed to by ptr [...]

Now, the following is perfectly valid, although `a' is not an array.

int a;
fread(&a, sizeof a, 1, fp);

Were it not for the exception quoted above such an interpretation
might be questionable. As it is, it's the only reasonable way to
interpret this aspect of the standard.

Jeremy.
Nov 13 '05 #49
Dan Pop wrote:
In <sl************ *******@ekoi.cl .cam.ac.uk> Jeremy Yallop <je****@jdyallo p.freeserve.co. uk> writes:
Dan Pop wrote:
An implementation doing array bounds checking *can* detect that the end
of the array has been reached without encountering any null character.
At this point, the implementation is free to do anything it wants,
including making demons fly out of your nose.


I find this a bit upsetting, if true. This means that we can have two
pointers that compare equal, one of which is known to point to a valid
object, and yet dereferencing the other has undefined behaviour.


Yup, C99 *explicitly* mentions this possibility:


It seems that you're right. It is pretty counterintuitiv e (if you
have the wrong intuitions, I suppose).
assert (p == q);


What for?!? Don't you trust the compiler to get the exit condition from
loop1 right or do you suspect that both != and == can evaluate to false
on the same pointer operands?


It was just for documentation, really. Perhaps a comment would have
been clearer. I didn't expect the assertion to fail (but then I don't
write assertions that I expect to fail).

Jeremy.
Nov 13 '05 #50

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
6402
by: Don Freeman | last post by:
Seems like whatever value I use for the first int field (starting position) the substring procedure negates it and triggers a String index out of range error. I've tried all sorts of work arounds to no avail, am I defining the two string variable incorrectly? code snippet: String lastChar = new String();
7
8590
by: Radhika Sambamurti | last post by:
Hi, I've written a substring function. The prototype is: int substr(char s1, char s2) Returns 1 if s2 is a substring of s1, else it returns 0. I have written this program, but Im sure there is an easier way to do this. I am hoping someone can point me to a more elegant way of writing this same function. //// code follows /////////// //implement a substring function, where if string2 is a sub of string 1, then the value returned is 1...
1
8115
by: sysindex | last post by:
I am trying to find a way to dynamically retrieve the substring starting point of an nText field. My query looks something like SELECT ID,Substring(DOCTEXT,0,200) from mytable where DOCTEXT like 'claim%'" This query has substring starting point set to 0. Is there a way to determine the starting point based on the first occurrence of the
11
5369
by: Darren Anderson | last post by:
I have a function that I've tried using in an if then statement and I've found that no matter how much reworking I do with the code, the expected result is incorrect. the code: If Not (strIn.Substring(410, 10).Trim = "") Then 'Something processed Else 'Something processed
5
2942
by: btober | last post by:
I can't seem to get right the regular expression for parsing data like these four sample rows (names and addresses changed to ficticious values) from a text-type column: Yolanda Harris, 38, of 40 South Main St., Newtown City, was charged Sunday with breach of peace and interfering with a police officer. Allen K. George, 30, of 88 Beverly Court was charged Saturday with possession of marijuana, third-degree criminal mischief, breach of...
2
6421
by: mallard134 | last post by:
Could someone please help a newbee vb programmer with a question that is driving me crazy. I am trying to understand a line of code that is supposed to return the domain portion of a valid email address. The following code works! ****************************************************************** strEmail = "j...@mydomain.com" String2 = strEmail.Substring(strEmail.IndexOf("@") + 1, _
4
3266
by: Jean-François Michaud | last post by:
Hello, I've been looking at this for a bit now and I don't see what's wrong with the code. Can anybody see a problem with this? Here is an XSLT snippet I use. <xsl:template match="graphic"> <xsl:param name="path" /> <fo:block padding-after="12pt">
6
9276
by: kellygreer1 | last post by:
What is a good one line method for doing a "length safe" String.Substring? The VB classes offer up the old Left function so that string s = Microsoft.VisualBasic.Left("kelly",200) // s will = "kelly" with no error // but string s2 = "kelly".Substring(0,200) // results in // ArgumentOutOfRangeException
11
7065
by: dyc | last post by:
how do i make use of substring method in order to extract the specified data from a a long string? I also need to do some checking b4 extracting the data, for instance: it only will extract the data when the first 8bits are 10101010,then it ll extract the following...for example: 10101010111000111 here is my code: Dim dataE As String Dim lot101, lot102, lot103, lot104 As String
3
2594
by: =?Utf-8?B?anAybXNmdA==?= | last post by:
Two part question: 1. Is Regex more efficient than manually comparing values using Substring? 2. I've never created a Regex expression. How would I use regex to do the equivalent of what I have coded using Substrings below? string s = TextBox1.Text.ToUpper(); string ch = s.Substring(0, 1); // read first character if ((ch == "B") || (ch == "C") || (ch == "X")) {
0
9903
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
11044
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9526
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7927
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7084
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5754
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5952
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4567
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4168
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.