string question

D

David Schwartz

I have a string A, and I want to write a substring (of varying lengths)
starting at the beginning of A, to another string.

Can someone show me how to do that?
 
A

Arthur J. O'Dwyer

I have a string A, and I want to write a substring (of varying lengths)
starting at the beginning of A, to another string.

Can someone show me how to do that?

Yes, and they already have. Try reading the responses you
got in
-Arthur
 
V

Vijay Kumar R Zanvar

David Schwartz said:
I have a string A, and I want to write a substring (of varying lengths)
starting at the beginning of A, to another string.

Can someone show me how to do that?

#if !defined ( __STDC__ ) || !defined ( __STDC_VERSION__ )
#error The compiler is not C99 compliant
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

#define TIMES 5

int
main ( void )
{
const char *str = ( const char [] ) { "Calvin and Hobbes" };
srand ( time ( NULL ) );

int len = strlen ( str );
char *another_string = malloc ( len + 1 );

if ( another_string )
{
for ( int i = 0; i < TIMES; i++ )
{
int rand_len = rand () % len;
rand_len = snprintf ( another_string, rand_len, "%s", str );
another_string[rand_len] = '\0'; /* not really required */
puts ( another_string );
}
free ( another_string );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
 
R

RoSsIaCrIiLoIA

I have a string A, and I want to write a substring (of varying lengths)
starting at the beginning of A, to another string.

Can someone show me how to do that?

#include <stdio.h >
#include <string.h>
#include <stdlib.h>
#include <time.h >
#include <stddef.h>
#include <assert.h>

char* sub(char* b, char* a, size_t lenb, size_t lena)
{size_t i;
char *bb;

assert(a!=NULL && b!=NULL && lena>=lenb);// how use ptrdiff_t ?
for( bb=b+lenb ; b<bb ; ++b, ++a)
*b=*a;
*b='\0';
return b;
}


#define TIMES 5

int
main ( void )
{char *str = "Calvin and Hobbes";
int lena = strlen ( str ), i ;
char *another_string = malloc ( lena + 1 );

srand (time(NULL));

if ( another_string )
{for (i = 0; i < TIMES; i++ )
{sub(another_string, str, rand() % (lena+1), lena);
puts ( another_string );
}
free( another_string );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
 
A

Allan Bruce

David Schwartz said:
I have a string A, and I want to write a substring (of varying lengths)
starting at the beginning of A, to another string.

Can someone show me how to do that?

Guys, what happened to the KISS principle!

The simplest solution would be to ensure that A has enough space for both
strings first, along with another temporary buffer, and do this:

strcpy(Temp, B);
strcat(Temp, A);
strcpy(A, Temp);

to ensure there is enough space in the buffers use strlen().
HTH
Allan
 
R

RoSsIaCrIiLoIA

#include <stdio.h >
#include <string.h>
#include <stdlib.h>
#include <time.h >
#include <stddef.h>
#include <assert.h>

char* sub(char* b, size_t lenb, char* a, size_t lena)
{char *bb;

assert(a!=NULL && b!=NULL && lena>=lenb);
for( bb=b+lenb ; b<bb ; ++b, ++a)
*b=*a;
*b='\0';
return bb;
}


#define TIMES 50

int
main ( void )
{char *str = "Calvin and Hobbes";
int lena = strlen ( str ), i ;
char *another_string = malloc ( lena + 1 );

srand (time(NULL));

if ( another_string )
{for (i = 0; i < TIMES; i++ )
{sub(another_string, rand() % (lena+1), str, lena);
puts ( another_string );
}
free( another_string );
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
 
R

RoSsIaCrIiLoIA

I have a string A, and I want to write a substring (of varying lengths)
starting at the beginning of A, to another string.

Can someone show me how to do that?
#include <stdio.h >
#include <string.h>
#include <stdlib.h>
#include <time.h >
#include <stddef.h>
#include <assert.h>

char* sub(char* b, size_t lenb, char* a, size_t lena)
{char *bb;

assert(a!=NULL && b!=NULL && lena>=lenb);
for( bb=b + lenb ; b<bb ; ++b, ++a)
*b=*a;
*b='\0';
return bb - lenb;
}


#define TIMES 50

int
main ( void )
{char *str = "Calvin '\'and Hobbes";
int lena = strlen ( str ), i ;
char *another_string = malloc ( lena + 1 ) ;

if ( !another_string )
return EXIT_FAILURE;
srand (time(NULL));
for( i=0; i<TIMES; ++i )
puts( sub(another_string, rand()%(lena+1), str, lena) );

free( another_string );
return EXIT_SUCCESS;
}
 
P

Peter Shaggy Haywood

Groovy hepcat David Schwartz was jivin' on Fri, 05 Mar 2004 05:59:50
GMT in comp.lang.c.
string question's a cool scene! Dig it!
I have a string A, and I want to write a substring (of varying lengths)
starting at the beginning of A, to another string.

Can someone show me how to do that?

#include <stdio.h>
#include <assert.h>
#include <string.h>

/* copies 'len' characters from offset 'off' of string 'src' to 'dst'
returns dst (analogous to MID$ in BASIC) */
char *substr(char *dst, const char *src, int off, int len)
{
assert(NULL != dst);
assert(NULL != src);
assert(0 <= len);
assert(0 <= off);

sprintf(dst, "%.*s", len, src + off);
return dst;
}

/* copies rightmost 'len' characters from 'src' to 'dst'
returns dst (analogous to RIGHT$ in BASIC) */
char *rightstr(char *dst, const char *src, int len)
{
const char *p;
int sl;

assert(NULL != dst);
assert(NULL != src);
assert(0 <= len);

if(len >= (sl = strlen(src)))
p = src;
else
p = src + sl - len;

sprintf(dst, "%.*s", len, p);
return dst;
}

/* copies leftmost 'len' characters from 'src' to 'dst'
returns dst (analogous to LEFT$ in BASIC) */
char *leftstr(char *dst, const char *src, int len)
{
assert(NULL != dst);
assert(NULL != src);
assert(0 <= len);

sprintf(dst, "%.*s", len, src);
return dst;
}


--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,142
Messages
2,570,818
Members
47,362
Latest member
eitamoro

Latest Threads

Top