Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to get string from front of array to position x?

Reply
Thread Tools

How to get string from front of array to position x?

 
 
Angus
Guest
Posts: n/a
 
      01-22-2011
Hi

If I want to eg print a string from position 3 to the end of the
string I can do this:
char szMessage[] = "Hello World";
printf("string fragment: %s\n", szMessage+5);

But if I just want to print "Hello" how can I do it. I can make a
copy but what other way to do this?

Angus


 
Reply With Quote
 
 
 
 
Willem
Guest
Posts: n/a
 
      01-22-2011
Angus wrote:
) If I want to eg print a string from position 3 to the end of the
) string I can do this:
) char szMessage[] = "Hello World";
) printf("string fragment: %s\n", szMessage+5);
)
) But if I just want to print "Hello" how can I do it. I can make a
) copy but what other way to do this?

The width/precision specifiers for the printf formats work on strings
as well. (Note that there are two specifiers, separated by a dot.)
As a bonus, you can use * for a specifier, and then give the value
in the argument list.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      01-22-2011
On 1/22/2011 2:02 PM, Angus wrote:
> Hi
>
> If I want to eg print a string from position 3 to the end of the
> string I can do this:
> char szMessage[] = "Hello World";
> printf("string fragment: %s\n", szMessage+5);


That would print from position 5 (the ' ') onward, not position 3.

> But if I just want to print "Hello" how can I do it. I can make a
> copy but what other way to do this?


One way is to use the "precision" modifier in the format:

printf ("string fragment: %.5s\n", szMessage);

The ".5" instructs printf() to print no more than 5 characters,
even if the argument string is longer. (If it's shorter, printf()
will print the entire string.)

--
Eric Sosman
http://www.velocityreviews.com/forums/(E-Mail Removed)lid
 
Reply With Quote
 
lawrence.jones@siemens.com
Guest
Posts: n/a
 
      01-22-2011
Angus <(E-Mail Removed)> wrote:
>
> If I want to eg print a string from position 3 to the end of the
> string I can do this:
> char szMessage[] = "Hello World";
> printf("string fragment: %s\n", szMessage+5);


Actually, that's position 5 (or 6, depending on how you count).

> But if I just want to print "Hello" how can I do it. I can make a
> copy but what other way to do this?


printf("string fragment: %.5s\n", szMessage);

Don't you have a reference that includes printf format specifiers?
--
Larry Jones

From now on, I'm devoting myself to the cultivation of
interpersonal relationships. -- Calvin
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Each Array Method Skipping first Array position Chris R. Ruby 3 01-28-2011 04:00 PM
Where is Form Relative Position and Absolute Position in VS.Net 2005 ? Luqman ASP .Net 1 02-07-2006 10:27 AM
position image based on document position edouard.lauer@pt.lu Javascript 3 01-14-2006 06:04 PM
How to set position of a web control depending on other control's position at run-time? James Wong ASP .Net Web Controls 4 07-14-2004 10:24 AM



Advertisments