i have a string and i want the first 5 chars to be removed
this is what i have
void shift(char *string) {
int i;
for(i=0;i<5;i++) {
while(1) {
*string = *(string+1);
if (*string=='\0') break;
}
}
}
its not working, i think it maybe because my string isnt terminated properly, but anyways can you see if this function is right?
---------- Post updated 04-03-11 at 12:06 AM ---------- Previous update was 04-02-11 at 11:17 PM ----------
nvm i got it using
void shift(char *string) {
int i, k;
char *h=string;
int j=strlen(string);
for(k=0;k<5;k++) {
for(i=0;i<j;i++) {
*string = *(string+1);
string++;
}
string=h;
j=strlen(string);
}
}
This is one way of manyto whack off the first n chars of string:
char *whack(char *src, size_t start)
{
if(strlen(src) >= start)
{
char *p=strdup(src);
p+=start;
strcpy(src, p);
free(p);
}
return src;
}
This is one way of manyto whack off the first n chars of string:
char *whack(char *src, size_t start)
{
if(strlen(src) >= start)
{
char *p=strdup(src);
p+=start;
strcpy(src, p);
free(p); // p has been modified
}
return src;
}
Calling "free(p)" after "p+=start" will corrupt the heap.
My shot:
char *shift( char *str, size_t count )
{
size_t len;
if ( NULL == str )
{
return( NULL );
}
len = strlen( str );
if ( len > count )
{
return( NULL );
}
memmove( str, str + cpunt, len - count + 1 );
return( str );
}
Fast but no error checking added!
void
shift(char *string)
{
char *ptr = string;
while (*(ptr+5)) {
*ptr = *(ptr+5);
ptr++;
}
*ptr = '\0';
}
The dumb+easy way is to use pointer math:
const char *str="0123456789";
printf("Entire string is %s\n", str);
printf("With 5 lopped off the front: %s\n", str+5);
Superfast, too. One instruction beats copying an entire string.
achenle:
Calling "free(p)" after "p+=start" will corrupt the heap.
My shot:
char *shift( char *str, size_t count )
{
size_t len;
if ( NULL == str )
{
return( NULL );
}
len = strlen( str );
if ( len > count )
{
return( NULL );
}
memmove( str, str + cpunt, len - count + 1 );
return( str );
}
should be
if (count > len)
Thanks,
Gaurav.
---------- Post updated at 03:08 PM ---------- Previous update was at 02:57 PM ----------
Another way ,
void * shiftbyn( char *str, size_t count )
{
size_t len = 0;
char *newstr = NULL;
if ( NULL == str )
{
return NULL;
}
len = strlen( str );
if ( count > len )
{
return NULL;
}
newstr = malloc (strlen(str) - count);
strcpy (newstr, str + count);
free (str); str = NULL;
return newstr;
}
Thanks,
Gaurav.
wouldnt this work, since strings are pointers?
buf=buf+5;
and whats the difference between declaring a string like
char *buf;
and
char buf[10];
arent they both pointers? But in some cases the way i declare it makes a difference through...
omega666:
wouldnt this work, since strings are pointers?
buf=buf+5;
and whats the difference between declaring a string like
char *buf;
and
char buf[10];
arent they both pointers? But in some cases the way i declare it makes a difference through...
Strings are NOT pointers. Pointer airthmetic (buf = buf + 5) can only be applied if buf is a pointer (char *buf) and not if it is a string (char buf [20] ) . If you want to do pointer airthmetic on a string, get a pointer to point to the base of the string and perform pointer airthmetic on the "pointer".
char buf [20]; //string
char *pointer_to_buf = buf // char pointer to the base of
// string "buf"
pointer_to_buf += 5; // gets the pointer to point to the
// address of buf [4]
Read K&R (Pointers and Arrays - chapter 5)
Thanks,
Gaurav.