Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I take a part of a string ?

Status
Not open for further replies.

lcurvo

Programmer
Apr 12, 2001
3
BR
How can I take a part of a string ?
The only way that I found is by the pointers. Is it correct ?
Is there no one way to take a part of a string ?
My problem is listing below, if you can help, answer me, please.

pt=DescCliente;
aa=(char)pt[0];

sprintf (Buffer,"441100003700180%c%c",aa,pt[0]);
MandaStringTerm (Buffer);

if (pt[0]=="Q")-> this not Work ? Why?
MandaStringTerm("392200200450375QUALIDADE ASSEGURADA");
 
It does not work, because pt[0] is a char, but "Q" is a string. Make it : 'Q' instead
And there is another way to take part of the string. You have to address every char from the initial string in the loop by its index, and assign to a new string variable, or char array.
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
You can use strncpy(), passing it an offset from the start of the string you're copying from. To give you an idea:

char source[]="hello chipmunk";
char dest[5];

Say, you wanted to copy just "chip" to dest:

strncpy(dest,source+6,4);
dest[4]=0;

This is essentially what I think aphrodita is saying in the 2nd paragraph of her(?) post, except you have strncpy() do the work for you.

Russ
bobbitts@hotmail.com
 
Was not it strcpy() to copy a string ?
and dest[4] = 0; is suspicios, it might say that the operands are not of the same type,
use dest[4] = '\0';
Best Regards,

aphrodita@mail.krovatka.ru {uiuc rules}
 
strcpy() copies an entire string from the 1st element up to the terminating null, strncpy() copies part of a string, how many bytes are specified in the 3rd argument. See your manual for strcpy() and strncpy().

0 and '\0' are equivalent, so there's nothing suspicious here. It may be that '\0' is a more common idiom, but using 0 in these contexts is perfectly correct. I don't know what you mean by saying the "operands are not of the same type."

Russ
bobbitts@hotmail.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top