Hi,
if i had a string (Iamhere) stored in an array of characters, and i want to get just (amhere) in another array, is there a way of doing it?? I've been reading in the string functions but couldn't figure out how this is done?
Thanks
Hi,
if i had a string (Iamhere) stored in an array of characters, and i want to get just (amhere) in another array, is there a way of doing it?? I've been reading in the string functions but couldn't figure out how this is done?
Thanks
Thank you,
but isn't strstr used when a string is already known, i'm trying to find a general case to start copying from the second element but it doesn't seem to be working i wrote this piece of code
/*get the required text after the operation*/
char mystring[100];
char *text;
length=strlen(mystring)-1;
/*to copy everything after the first character*/
memcpy(text, mystring+1, length);
/*make last element is a null*/
text[length]='/0';
printf("Substring is %s\n",text);
The code returns the substring but also returns garbage after it
A little more information always helps.
#include <stdio.h>
#include <string.h>
int main( void )
{
char src[] = "Iamhere";
char dst[ sizeof src - 1];
char *result;
/* point to the next char in string */
result = &src[1];
/* copy string */
strcpy( dst, result );
/* test destination string */
printf( "dst = %s\n", dst );
return 0;
}
Thank you very much, it worked perfect =)
Thank you very much, it worked perfect =)
It all depends of what you want to do with it.
You can even get away without a pointer for less typing.
#include <stdio.h>
#include <string.h>
int main( void )
{
char text[] = "Iamhere";
char subs[ sizeof text - 1 ];
strcpy( subs, &text[1] );
printf( "subs says %s\n", subs );
return 0;
}
Thank you,
Another question i've been trying to do it as a separate function but the compiler gives back an error
char *returntext(char t[])
{ char text[100], *ftext;
text=&t[1];
strcpy(ftext,text);
return (ftext);
}
where t was originally an array of 100 elements,
gcc returns
incompatible types in assignment
where t was originally an array of 100 elements,
gcc returns
incompatible types in assignment
text=&t[1];
---> text is an array of chars and you are trying to use it as a pointer.
Furthermore you have to be carefully with trying to return pointers to variables created inside a function block, since as soon as the function is finished that variable doesn't exist anymore.
i.e. char text[100]
will disappear when returntext()
is finished.
strcpy(ftext,text);
I am afraid that won't work neither. ftext is an uninitialized pointer, who knows to what is pointing to. Trying to change the content of what ftext points to will produce a segmentation fault.
Let me see if I can help you without divulging the actual code.
I believe you can benefit of looking at the function strcpy() as an example. Essentially, your customized function need to be prototyped the same way.
char *strcpy( char *s1, char *s2 );
strcpy accepts a pointer to a destination string and a pointer to a source string, both variables need to exist before you can pass it to strcpy; and only then it will do its work on them and return a pointer to the destination string, which still exist beforehand.
Hope that helps.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.