473,854 Members | 1,847 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 31946


pete wrote:
>>char s1[3] = "123";

In that case s1, satisfies the defintion for "pointer to a string"

N869

7.1.1 Definitions of terms

[#1] A string is a contiguous sequence of characters
terminated by and including the first null character. The
term multibyte string is sometimes used instead to emphasize
special processing given to multibyte characters contained
in the string or to avoid confusion with a wide string. A
pointer to a string is a pointer to its initial (lowest
addressed) character.


No.
Consider the declaration and initialization.
char s1[3] = "123";

You have declared an array of 3 characters and assigned the characters
'1','2','3' to this array. s1[0] has the value '1'. s1[1] has the
value '2'. s1[3] has the value '3'.

Where in this character array is there a contigous sequence of
characters terminated by and including the first null character?

Answer: There is no null character in the array, thus the array
does not represent a string.

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

Nov 13 '05 #21
Al Bowers wrote:

pete wrote:
>>>char s1[3] = "123";
In that case s1, satisfies the defintion for "pointer to a string"

N869

7.1.1 Definitions of terms

[#1] A string is a contiguous sequence of characters
terminated by and including the first null character. The
term multibyte string is sometimes used instead to emphasize
special processing given to multibyte characters contained
in the string or to avoid confusion with a wide string. A
pointer to a string is a pointer to its initial (lowest
addressed) character.


No.
Consider the declaration and initialization.
char s1[3] = "123";

You have declared an array of 3 characters and assigned the characters
'1','2','3' to this array. s1[0] has the value '1'. s1[1] has the
value '2'. s1[3] has the value '3'.

Where in this character array is there a contigous sequence of
characters terminated by and including the first null character?


Nowhere in the array.
However, since strings are not confined to arrays,
what difference does it make ?
Answer: There is no null character in the array, thus the array
does not represent a string.


That's been my point all along.
But you snipped the relevant part of the post,
which describes the specific case under dsicussion.
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);
}

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

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #23
On 4 Nov 2003 17:59:19 GMT, Da*****@cern.ch (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
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.


How is it different from the puts() call above? Surely, "array" means a
consecutive "string" of characters, not a C array, otherwise, char *s1 coming
from a successful calloc() would cause undefined behaviour right away when fed
to printf().

Nov 13 '05 #24
On Tue, 04 Nov 2003 15:50:57 GMT, in comp.lang.c , pete
<pf*****@mindsp ring.com> wrote:
Al Bowers wrote:

Where in this character array is there a contigous sequence of
characters terminated by and including the first null character?


Nowhere in the array.
However, since strings are not confined to arrays,
what difference does it make ?


It makes the difference that puts requires a string, and s1 is not a
string since it has no null terminator. So when puts is putting chars
to stdout, it will read from memory beyond the region allocated to s1,
and this is disallowed.

The fact that somewhere in memory nearby there is a null doesn't mean
that magically s1 becomes a string. It merely means that puts will by
good luck stop sending data to stdout.
Answer: There is no null character in the array, thus the array
does not represent a string.


That's been my point all along.


I'm confused. Are you agreeing that this is UB or not?

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.angelfire.c om/ms3/bchambless0/welcome_to_clc. html>
----== Posted via Newsfeed.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeed.com The #1 Newsgroup Service in the World! >100,000 Newsgroups
---= 19 East/West-Coast Specialized Servers - Total Privacy via Encryption =---
Nov 13 '05 #25
Mark McIntyre wrote:
Answer: There is no null character in the array, thus the array
does not represent a string.


That's been my point all along.

I'm confused. Are you agreeing that this is UB or not?


I think it is quite clear that this is not as clear as one might
wish.

Have a closer look at the examples given and note that s1 and
s2 indeed does constitute a string if they happen to be
contigous in memory. In particular note that the standard
does not include the mention of the word array when it defines
the term string.

--
Thomas.

Nov 13 '05 #26
rihad wrote:
Da*****@cern.ch (Dan Pop) wrote:
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 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.


How is it different from the puts() call above? Surely, "array" means
a consecutive "string" of characters, not a C array, otherwise, char
*s1 coming from a successful calloc() would cause undefined behaviour
right away when fed to printf().


Very simply. The first checks the contiguity of the two arrays
(not guaranteed) before using the first as a string. The second
simply uses it as a string. The expression "s1 + sizeof s1" is
specifically valid because it points one beyond the actual array.
The expression s2 is valid by definition. You can replace the
puts() call with the printf() call (and vice-versa) without
altering the validity/invalidity of the two fragments.

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 13 '05 #27

"Dan Pop" <Da*****@cern.c h> schrieb im Newsbeitrag
news:bo******** **@sunnews.cern .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.
Do you know any possibility for an implementation to produce different
results (provided the expression "s2 == s1 + sizeof s1" yields true)?

Robert
Nov 13 '05 #28


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
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.

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

Nov 13 '05 #29
In <c1************ *************** *****@4ax.com> rihad <ri***@mail.r u> writes:
On 4 Nov 2003 17:59:19 GMT, Da*****@cern.ch (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.


How is it different from the puts() call above?


puts doesn't expect an array, it merely expects a string.

The puts function writes the string pointed to by s to the stream
pointed to by stdout, and appends a new-line character to the output.
The terminating null character is not written.
Surely, "array" means a
consecutive "string" of characters, not a C array, otherwise, char *s1 coming
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.
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.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #30

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
9752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11041
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
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...
0
9525
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...
0
5753
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
5946
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4565
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
3
3193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.