473,856 Members | 2,133 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 31950
In <3f************ ***********@new sreader02.highw ay.telekom.at> "Robert Stankowic" <pc******@netwa y.at> writes:

"Dan Pop" <Da*****@cern.c h> schrieb im Newsbeitrag
news:bo******* ***@sunnews.cer n.ch...
In <3F***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:
>Specifically , we're discussing the case where the program
>has determined whether or not s1 and s2 are contiguous,
>and only the case where they are contiguous.
>
>char s1[3] = "123";
>char s2[4] = "456";
>
>if (s2 == s1 + sizeof s1) {
^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^
Just curious - if I don't misunderstand 6.5.9 verse 6 (in N869) the above
statement is correct, and if the expression "s2 == s1 + sizeof s1" yields
true, s1 and s2 form a continguous sequence of 7 char objects which contains
a '\0' in the last position.
Isn't in this case the resulting object the same (except of scope) as if we
wrote
char *s1 = malloc(7);
if(s1)
{
strcpy(s1, "123456");
}
Here we also did not explicitely define an array, but we definitely created
a string.


The malloc call has created a *single* object that can be treated as an
array of 7 char, while the definitions of s1 and s2 create two different
objects that cannot be treated as a single object, even if adjacent.
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.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #31
In <bo************ *@ID-169908.news.uni-berlin.de> Al Bowers <xa******@rapid sys.com> writes:


Dan Pop wrote:
In <3F***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:

Specifically , we're discussing the case where the program
has determined whether or not s1 and s2 are contiguous,
and only the case where they are contiguous.

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

if (s2 == s1 + sizeof s1) {
puts(s1);
}

I entirely agree that, according to the wording of the standard, this
code snippet is correct. There is no consensus in comp.std.c on whether
this is the intent of the standard or not, but the actual wording is
unambiguous. However, if you replace the puts call by a printf call:

printf("%s\n", s1);

you're right into undefined behaviour, because:

s The argument shall be a pointer to an array of character type.
Characters from the array are written up to (but not including) a
terminating null character; if the precision is specified, no more
than that many characters are written. If the precision is not
specified or is greater than the size of the array, the array shall
contain a null character.


What about some of the string handling functions?

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

if (s2 == s1 + sizeof s1) {
strcpy(buf,s1);
}

In section:
7.21.1 String function conventions
You have:
If an array is accessed beyond the end of an object, the behavior is
undefined.


There is an unfortunate conflict between your identifiers and the
parameter names used by the standard in the description of strcpy.
I'm using s1 and s2 with their meanings in the C standard, below.

The strcpy function copies the string pointed to by s2 (including
^^^^^^^^^^
the terminating null character) into the array pointed to by s1.
^^^^^^^^^

Still no problem, since only the s1 argument is supposed to point to an
array. And buf is large enough to hold a 6 character string.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #32
On 5 Nov 2003 11:52:35 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <c1************ *************** *****@4ax.com> rihad <ri***@mail.r u> writes:
Surely, "array" means a
consecutive "string" of characters, not a C array, otherwise, char *s1 coming


(By a "C array" I meant char a[] = "hello"; printf("%s\n", a);)
Array means whatever the standard defines as an array:

* An array type describes a contiguously allocated set of objects
with a particular member object type, called the element type. Array
types are characterized by their element type and by the number of
members of the array.


Then given char *p = calloc(1, 1); p points to an array of one char (barring
nomem)? And given char c = 0; &c points to an array of one char? Sorry, but I
read the above as if int i; meant array of 1 int.
from a successful calloc() would cause undefined behaviour right away when fed
to printf().


Wrong:

The calloc function allocates space for an array of nmemb objects,
^^^^^
each of whose size is size.


Sorry, but I *really* fail to understand why substituting the puts(s1); call
below with printf("%s\n", s1); suddenly invokes undefined behaviour, as you have
pointed out. &s1[0] points to an array of objects. The array is ended by a
((char) 0). Nowhere in the range of [ (s1 + 0) .. (s1 + sizeof s1 + sizeof s2) )
is an uninitialized object being accessed for reading (assuming the if holds
true, which is just a compile time constant IIRC).

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

if (s2 == s1 + sizeof s1) {
puts(s1);
}

Nov 13 '05 #33


Dan Pop wrote:
In <bo************ *@ID-169908.news.uni-berlin.de> Al Bowers <xa******@rapid sys.com> writes:
Dan Pop wrote:
In <3F***********@ mindspring.com> pete <pf*****@mindsp ring.com> writes:

Specificall y, we're discussing the case where the program
has determined whether or not s1 and s2 are contiguous,
and only the case where they are contiguous.

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

if (s2 == s1 + sizeof s1) {
puts(s1);
}
I entirely agree that, according to the wording of the standard, this
code snippet is correct. There is no consensus in comp.std.c on whether
this is the intent of the standard or not, but the actual wording is
unambiguou s. However, if you replace the puts call by a printf call:

printf("%s\n", s1);

you're right into undefined behaviour, because:

s The argument shall be a pointer to an array of character type.
Characters from the array are written up to (but not including) a
terminating null character; if the precision is specified, no more
than that many characters are written. If the precision is not
specified or is greater than the size of the array, the array shall
contain a null character.


What about some of the string handling functions?

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

if (s2 == s1 + sizeof s1) {
strcpy(buf,s1);
}

In section:
7.21.1 String function conventions
You have:
If an array is accessed beyond the end of an object, the behavior is
undefined.

There is an unfortunate conflict between your identifiers and the
parameter names used by the standard in the description of strcpy.
I'm using s1 and s2 with their meanings in the C standard, below.

The strcpy function copies the string pointed to by s2 (including
^^^^^^^^^^
the terminating null character) into the array pointed to by s1.
^^^^^^^^^

Still no problem, since only the s1 argument is supposed to point to an
array. And buf is large enough to hold a 6 character string.

Dan


I agree.
What about?

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

if (s2 == s1 + sizeof s1) {
strcpy(s1,"Hell o");
}
Assuming equality(the equality expression yields 1).

--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 13 '05 #34
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";


--
pete
Nov 13 '05 #35
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";
Given this:

char s[] = "123456", (*p3)[3] = &s;

is calling

printf("%s\n", p3[0]);

illegal, but

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

is legal?

I'm pretty sure they are both legal, because nowhere is unowned/uninitialized
memory being accessed. Then why can't we assume that in the case of

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

and assert(s2 == s1 + sizeof s1);

there's some virtual object s that spans the two objects s1 and s2 and that
object s consitutes a valid C string?

Nov 13 '05 #36
In <da************ *************** *****@4ax.com> rihad <ri***@mail.r u> writes:
On 5 Nov 2003 11:52:35 GMT, Da*****@cern.ch (Dan Pop) wrote:
In <c1************ *************** *****@4ax.com> rihad <ri***@mail.r u> writes:
Surely, "array" means a
consecutiv e "string" of characters, not a C array, otherwise, char *s1 coming
(By a "C array" I meant char a[] = "hello"; printf("%s\n", a);)
C array is *everything* the standard defines as such.
Array means whatever the standard defines as an array:

* An array type describes a contiguously allocated set of objects
with a particular member object type, called the element type. Array
types are characterized by their element type and by the number of
members of the array.


Then given char *p = calloc(1, 1); p points to an array of one char (barring
nomem)?


Yes, in common parlance. A pedant would say that p points to the first
character of an array of one char. However, given the type of p, there
is no place for confusion if one simply uses your wording.
And given char c = 0; &c points to an array of one char?
Yes.
Sorry, but I read the above as if int i; meant array of 1 int.
This is correct, too.

7 For the purposes of these operators, a pointer to an object that
is not an element of an array behaves the same as a pointer to
the first element of an array of length one with the type of
the object as its element type.
from a successful calloc() would cause undefined behaviour right away when fed
to printf().


Wrong:

The calloc function allocates space for an array of nmemb objects,
^^^^^
each of whose size is size.


Sorry, but I *really* fail to understand why substituting the puts(s1); call
below with printf("%s\n", s1); suddenly invokes undefined behaviour, as you have
pointed out. &s1[0] points to an array of objects. The array is ended by a
((char) 0).


Nope. The array is ended by a character that is NOT a null character.
It is *only* the s2 array that ends with a null character.
Nowhere in the range of [ (s1 + 0) .. (s1 + sizeof s1 + sizeof s2) )
is an uninitialized object being accessed for reading (assuming the if holds
true, which is just a compile time constant IIRC).
It doesn't matter. The standard clearly states that %s expects an array
containing a null character. There is no such character in the s1 array,
therefore the printf call invokes undefined behaviour. It's as simple as
that, whether you get it or not.

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.
char s1[3] = "123";
char s2[4] = "456";

if (s2 == s1 + sizeof s1) {
puts(s1);
}


OTOH, this is fine because puts() does NOT expect an array. It expects
a sequence of characters terminated by a null characters and it does not
care about how this sequence of characters is allocated. Reread the
definition of "string".

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #37
In <bo************ *@ID-169908.news.uni-berlin.de> Al Bowers <xa******@rapid sys.com> writes:
What about?

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

if (s2 == s1 + sizeof s1) {
strcpy(s1,"Hell o");
}
Assuming equality(the equality expression yields 1).


An obvious case of undefined behaviour: you're writing beyond the end of
the s1 array. A bounds checking implementation is not supposed to be
impressed by the fact that s2 == s1 + sizeof s1.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #38
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.


This makes no sense...
char s1[3] = "123";
char s2[4] = "456";
Given this:

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

is calling

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

illegal, but

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

is legal?

I'm pretty sure they are both legal, because nowhere is unowned/uninitialized
memory being accessed. Then why can't we assume that in the case of
Neither is legal.

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

and assert(s2 == s1 + sizeof s1);

In this case s1 and s2 is a valid string. printf expect a null
terminated array. A null terminated array is always a string, but
a string is not necessarily a null terminated array.
there's some virtual object s that spans the two objects s1 and s2 and that
object s consitutes a valid C string?


The definition of a string in C never mentions object nor array. Just
a contigous sequence of chars of which the last one is 0.

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

--
Thomas.

Nov 13 '05 #39
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.

Jeremy.
Nov 13 '05 #40

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
10692
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10769
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
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
5953
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.