Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > substring assignment in fortran, C, etc.

Reply
Thread Tools

substring assignment in fortran, C, etc.

 
 
LC's No-Spam Newsreading account
Guest
Posts: n/a
 
      05-19-2009
I am trying to collect equivalent statements in various languages.

I am now dealing with substring assignment.

Let us assume that string="abcd", value="AB", i=2, j=3 (I won't
scandalize if in your favourite language it is i=1 j=2 :=). Nor if one
has to use n=j-i+1 (length("AB")).

I want an assignment which returns string="aABd"

I started from Fortran string(i:j)=value

I came out with the examples below, but cannot find a satisfactory one
for C using standard library functions.

IDL: strput,string,value,i

Java: string=string.substring(0,i)+value+string.substrin g(j+1) ;
or using string buffers
StringBuffer sb = new StringBuffer(string) ;
sb.replace(i,j+1,value) ;

awk: string=substr(string,1,i-1) value substr(a,j+1)

mysql: set @string:=insert(@string,@i,@n,@value)

Postscript: string i value putinterval /string exch def

I even have csh

@ i=$i-1
@ j=$j+1
set string=`echo $string | cut -c-$i`$value`echo $string | cut -c$j-`

But what about C ?
I can find an hardcoded solution for a character array

char a[5]="abcd" ;
a[1]='A';
a[2]='B';

which means I can possibly write some for loop

But what if I want a (or string) to be a "standard" string i.e. a
char *a ?


Please note followup to clf

--
----------------------------------------------------------------------
http://www.velocityreviews.com/forums/(E-Mail Removed) is a newsreading account used by more persons to
avoid unwanted spam. Any mail returning to this address will be rejected.
Users can disclose their e-mail address in the article if they wish so.
 
Reply With Quote
 
 
 
 
jameskuyper
Guest
Posts: n/a
 
      05-19-2009
Why was your message posted to comp.lang.c, but with followups
redirected only to comp.lang.fortran? Anyone who doesn't notice that
and fix it will be posting his response solely to c.l.f. Any such
message that contains a technical error will not be seen by any of the
C experts on c.l.c, the people most likely to be able to notice and
correct such an error.

LC's No-Spam Newsreading account wrote:
> I am trying to collect equivalent statements in various languages.
>
> I am now dealing with substring assignment.
>
> Let us assume that string="abcd", value="AB", i=2, j=3 (I won't
> scandalize if in your favourite language it is i=1 j=2 :=). Nor if one
> has to use n=j-i+1 (length("AB")).
>
> I want an assignment which returns string="aABd"
>
> I started from Fortran string(i:j)=value


The closest C equivalent involves a function call, rather than an
assignment. This is inherently the case, since a string is a data
format in C, and it is a format that is stored in an array. An array
cannot be assigned in C. You could create a C struct type that either
contains a fixed-size array, or points at an array whose size need not
be fixed. Such a struct could value could be assigned, but no such
struct type is part of standard C.
Here is the corresponding function call. It would use the i=1, j=2
option you mentioned above:

memcpy(string+i, value, j+1-i)

If string is the name of a pointer, rather than the name of an array,
you can make this an assignment if you want to:

string = memcpy(string+i, value, j+1-i);

However, there's no good reason to do so, because the assignment
doesn't actually change anything. If you modified the context, such as

newstring = memcpy(string+i, value, j+1-i);

then the assignment would no longer be pointless; but it would
probably indicate a design error.

....
> But what if I want a (or string) to be a "standard" string i.e. a
> char *a ?


In C, a string is a data format, not a type. That format consists of a
series of char objects, ending with a nul character '\0'. The type
char*a isn't a string type, it's a pointer to a character. That
character might or might not point to a character in a string. If it
does, that string might or might not take up the entire size of an
array of char.

If you're worried about the void* data type returned by memcpy(), you
can convert it:

(char*)memcpy(string+i, value, j+1-i)
 
Reply With Quote
 
 
 
 
Richard Maine
Guest
Posts: n/a
 
      05-19-2009
LC's No-Spam Newsreading account <(E-Mail Removed)> wrote:

> I came out with the examples below, but cannot find a satisfactory one
> for C using standard library functions.

....
> Please note followup to clf


Why? It is a question about how to do something in C. The only Fortran
in sight is just used as an example of what effect you want to achieve.
I don't even see why it was posted to comp.lang.fortran at all, much
less with followups directed solely there. Do you really expect
comp.lang.fortran to be the most appropriate place to discuss how to do
something in C? That makes it sound to me just like an invitation for
language flaming about how painful the Fortran folk might find the C way
of doing something.

--
Richard Maine | Good judgment comes from experience;
email: last name at domain . net | experience comes from bad judgment.
domain: summertriangle | -- Mark Twain
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      05-19-2009
In comp.lang.fortran jameskuyper <(E-Mail Removed)> wrote:
< Why was your message posted to comp.lang.c, but with followups
< redirected only to comp.lang.fortran? Anyone who doesn't notice that
< and fix it will be posting his response solely to c.l.f. Any such
< message that contains a technical error will not be seen by any of the
< C experts on c.l.c, the people most likely to be able to notice and
< correct such an error.

< LC's No-Spam Newsreading account wrote:
<> I am trying to collect equivalent statements in various languages.

<> I am now dealing with substring assignment.
(snip)

<> I want an assignment which returns string="aABd"

<> I started from Fortran string(i:j)=value

< The closest C equivalent involves a function call, rather than an
< assignment.

It involves function call syntax, though not necessarily (as I
understand the C standard) an actual function call.

< This is inherently the case, since a string is a data
< format in C, and it is a format that is stored in an array. An array
< cannot be assigned in C.

There has been discussion in comp.lang.fortran on the advantages
and disadvantages of function syntax vs. substring syntax.

There is a disadvantage to Fortran substring syntax, especially
in the lvalue case, in that it doesn't generalize. You can't,
for example, directly substring a substring:

string(i:j)(k:l) isn't legal.

(snip)

< Here is the corresponding function call. It would use the
< i=1, j=2 option you mentioned above:

< memcpy(string+i, value, j+1-i)

My choice would be strncpy(string+i, value, j+1-i);
but I agree that memcpy will also work. Keeping to the str...
functions for string work seems more consistent.

< If string is the name of a pointer, rather than the name of an
< array, you can make this an assignment if you want to:

(snip discussion about strings and pointers in C, not so
relevant to the OP question.)

As I mentioned in a post not follow up to comp.lang.c, the
PL/I form is:

substr(a,2,2)='AB'; or more generally

substr(string,i,j+1-i)=value;

Again, note function syntax but not a function.
(PL/I calls them pseudo-variables.) This is consistent
with the function call syntax in expressions.

-- glen
 
Reply With Quote
 
jameskuyper
Guest
Posts: n/a
 
      05-19-2009
glen herrmannsfeldt wrote:
> In comp.lang.fortran jameskuyper <(E-Mail Removed)> wrote:

....
> <> I want an assignment which returns string="aABd"
>
> <> I started from Fortran string(i:j)=value
>
> < The closest C equivalent involves a function call, rather than an
> < assignment.
>
> It involves function call syntax, though not necessarily (as I
> understand the C standard) an actual function call.


True; but it's a distinction of negligible importance; it's possible
for a C compiler to inline some or all of any call to a function if
the definition of that function is known to the compiler; highly
optimizing C compilers inline more code than you might expect. In
practice, it's simpler to just refer to them as function calls, and
not worry about the details of what the compiler actually does with
them.

....
> < Here is the corresponding function call. It would use the
> < i=1, j=2 option you mentioned above:
>
> < memcpy(string+i, value, j+1-i)
>
> My choice would be strncpy(string+i, value, j+1-i);
> but I agree that memcpy will also work. Keeping to the str...
> functions for string work seems more consistent.


The desired functionality is underdefined; the difference between your
version and mine matters only if "value" is shorter than the substring
it is replacing. My version has possible undefined behavior in that
case; yours avoids that, at the cost of being very marginally slower
for large sub strings. What does fortran code given above do in that
case?
 
Reply With Quote
 
glen herrmannsfeldt
Guest
Posts: n/a
 
      05-19-2009
In comp.lang.fortran jameskuyper <(E-Mail Removed)> wrote:
< glen herrmannsfeldt wrote:
<> In comp.lang.fortran jameskuyper <(E-Mail Removed)> wrote:
< ...

<> < The closest C equivalent involves a function call,
<> < rather than an assignment.

<> It involves function call syntax, though not necessarily (as I
<> understand the C standard) an actual function call.

< True; but it's a distinction of negligible importance; it's possible
< for a C compiler to inline some or all of any call to a function if
< the definition of that function is known to the compiler;

I would agree, except that the OP question is pretty much
a question of syntax, not of the underlying implementation.

The non-function-call syntax of the other languages mentioned
may actually be implemented as a function call.

< highly
< optimizing C compilers inline more code than you might expect. In
< practice, it's simpler to just refer to them as function calls, and
< not worry about the details of what the compiler actually does with
< them.

(snip on memcpy vs. strncpy)

< The desired functionality is underdefined; the difference between your
< version and mine matters only if "value" is shorter than the substring
< it is replacing. My version has possible undefined behavior in that
< case; yours avoids that, at the cost of being very marginally slower
< for large sub strings. What does fortran code given above do in that
< case?

The general rule is that Fortran pads with blanks. When CHARACTER
was added to Fortran in Fortran 77 the lengths were always known
at compile time. CHARACTER variables had a fixed length and were
padded with blanks when a shorter value was stored. I believe that
is true for the OP examples of substring assignment, but I am not
so sure in all possible cases.

-- glen
 
Reply With Quote
 
jameskuyper
Guest
Posts: n/a
 
      05-19-2009
glen herrmannsfeldt wrote:
....
> The general rule is that Fortran pads with blanks. When CHARACTER
> was added to Fortran in Fortran 77 the lengths were always known
> at compile time. CHARACTER variables had a fixed length and were
> padded with blanks when a shorter value was stored. I believe that
> is true for the OP examples of substring assignment, but I am not
> so sure in all possible cases.


The function call

sprintf(string+i, "%-*.*s%s", j+1-i, j+1-i, value, string+j);

would handle padding with blanks as you describe if value were too
short, if it weren't for the fact that it has undefined behavior
(because of the overlap between the output string and the input
string). If newstring were a seperate char array containing enough
space to store the result, or a pointer to the first element of such
an array, then

sprintf(newstring, "%*.*s%-*.*s%s", i, i, string, j+1-i, j+1-i,
value, string+j);

would do the job with well-defined behavior.
You can write C code to do just about anything you want with a string;
but C's built-in string-oriented capabilities are not in the same
league as those of, say, perl.
 
Reply With Quote
 
James Van Buskirk
Guest
Posts: n/a
 
      05-19-2009
"jameskuyper" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> LC's No-Spam Newsreading account wrote:
>> I am trying to collect equivalent statements in various languages.


>> I am now dealing with substring assignment.


>> Let us assume that string="abcd", value="AB", i=2, j=3 (I won't
>> scandalize if in your favourite language it is i=1 j=2 :=). Nor if one
>> has to use n=j-i+1 (length("AB")).


>> I want an assignment which returns string="aABd"


But why didn't you more properly torture c.l.c. with an example
like:

C:\gfortran\clf\Cstr>type Cstr.f90
program Cstr
implicit none
character(*), parameter :: abcd = 'abcd'
character(len(abcd)) string(
character(*), parameter :: AB = 'AB'
integer pos

string = abcd
pos = 2
string((posos+len(AB)-1) = AB
write(*,'(a)') string
end program Cstr

C:\gfortran\clf\Cstr>gfortran Cstr.f90 -oCstr

C:\gfortran\clf\Cstr>Cstr
aABd
aABd
aABd
aABd
aABd
aABd
aABd
aABd

?

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end


 
Reply With Quote
 
BartC
Guest
Posts: n/a
 
      05-20-2009

"James Van Buskirk" <(E-Mail Removed)> wrote in message
news:guvg2o$393$(E-Mail Removed)-september.org...
> "jameskuyper" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> LC's No-Spam Newsreading account wrote:
>>> I am trying to collect equivalent statements in various languages.

>
>>> I am now dealing with substring assignment.

>
>>> Let us assume that string="abcd", value="AB", i=2, j=3 (I won't
>>> scandalize if in your favourite language it is i=1 j=2 :=). Nor if one
>>> has to use n=j-i+1 (length("AB")).

>
>>> I want an assignment which returns string="aABd"

>
> But why didn't you more properly torture c.l.c. with an example
> like:


> character(*), parameter :: AB = 'AB'
> string((posos+len(AB)-1) = AB
> write(*,'(a)') string

....

I don't get this. Didn't Fortran use to be even more crude and basic than C?

So how did the Fortran committees manage to bring it into the 21st century
(and still call it Fortran), while C is still languishing in the 1970s it
seems?

--
Bart

 
Reply With Quote
 
LC's No-Spam Newsreading account
Guest
Posts: n/a
 
      05-20-2009
On Tue, 19 May 2009, Richard Maine wrote:
> LC's No-Spam Newsreading account wrote:
>
>> I came out with the examples below, but cannot find a satisfactory one
>> for C using standard library functions.

> ...
>> Please note followup to clf

>
> Why? It is a question about how to do something in C.


Apologies if this seemed inappropriate. My reasons were twofold. One is
that I read sort of regularly clf and keep it "caught up" while I do not
read regularly clc. The other one is that I wanted to get an answer from
somebody who was fluent in both languages (my question was not only "how
to do something in C" but "how to do in C something similar to the way
one does in Fortran"), say "the intersection of knowledgeable users on
clf and clc".

> Do you really expect comp.lang.fortran to be the most appropriate
> place to discuss how to do something in C? That makes it sound to me
> just like an invitation for language flaming


I wanted to avoid flames, and apparently have been successful, all the
replies are rather technical and up to the point ! Thanks !

--
----------------------------------------------------------------------
(E-Mail Removed) is a newsreading account used by more persons to
avoid unwanted spam. Any mail returning to this address will be rejected.
Users can disclose their e-mail address in the article if they wish so.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Assignment operator self-assignment check Chris C++ 34 09-26-2006 04:26 AM
Augument assignment versus regular assignment nagy Python 36 07-20-2006 07:24 PM
RegEx search for a substring within a substring colinhumber@gmail.com Perl Misc 3 08-03-2005 04:29 PM
find if there is a given substring inside a string juli ASP .Net 3 12-06-2004 11:52 AM
web sddress substring =?Utf-8?B?Sm9u?= ASP .Net 2 11-30-2004 01:31 PM



Advertisments