Current location: Hot Scripts Forums » Programming Languages » C/C++ » array question


array question

Reply
  #1 (permalink)  
Old 05-17-06, 08:18 AM
stealth04 stealth04 is offline
Newbie Coder
 
Join Date: Jun 2004
Posts: 20
Thanks: 0
Thanked 0 Times in 0 Posts
array question

Hi, I just posted it on C/C++ forum, because I have to do this piece of code in C but as PHP is similar and this forum is more active I'm posting it here again:

anyone knows how to select a piece of string from the array?
For example, let's say we have this: char mainstring[] = "This is a main string";
And I would like to display just a part of this string, for example the characters from mainstring[2] to mainstring[7].
Regards,
Niko
Reply With Quote
  #2 (permalink)  
Old 05-17-06, 08:40 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Quote:
Originally Posted by stealth04
Hi, I just posted it on C/C++ forum, because I have to do this piece of code in C but as PHP is similar and this forum is more active I'm posting it here again:
absolutly not true: php is totally different from C

anyway, try:
Code:
for(int i=0; i<mainstring.length; i++){
  for(int a=2; a<mainstring[i].length; a++){
   Console.writeline(mainstring[i][a]);
  }
}
i'm not familiar with the C syntax as you probably will recognise . but try this

Greetz,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 05-17-06, 11:16 AM
Crassius Crassius is offline
Newbie Coder
 
Join Date: Mar 2006
Location: Melbourne, FL
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Why not just use built in php functions like: substr and explode?

for example, if you want to separate the words in the string into an array do:
$yourarray = explode(" ", $mainstring); // This will make an array of the words because it's splits the string wherever there's a space (" ").

if you want to take a chunk out of the string do:
$chunk = substr($mainstring, a, b);
//a would be a number for which letter you want to start at.
//b would the number of characters until the chunk ends.
eg. $chunk = substr($mainstring, 2, 6);
this (using your string above) gives "is is "\

hope this helps, if not, check out the manual for both functions here:

http://us3.php.net/manual/en/function.explode.php
and
http://us3.php.net/manual/en/function.substr.php
__________________
~With great power comes great responsibility.~
Reply With Quote
  #4 (permalink)  
Old 05-17-06, 12:14 PM
Christian's Avatar
Christian Christian is offline
Community VIP
 
Join Date: Mar 2005
Location: ProgrammingTalk
Posts: 2,449
Thanks: 0
Thanked 6 Times in 5 Posts
Moved to C/C++ ....

Somebody will get around and help you there!

BTW, please don't cross post!
__________________
:: ImperialBB :: New version in the works! :: http://www.imperialbb.com ::

:: Have a question about the board? The Rules? An Infraction/Warning? :: Contact Form ::
Reply With Quote
  #5 (permalink)  
Old 05-17-06, 02:35 PM
King Coder King Coder is offline
Community VIP
 
Join Date: Jan 2006
Posts: 703
Thanks: 0
Thanked 0 Times in 0 Posts
Well, you have the array, so just print out what you want.... Remember, char arrsys will end with a terminating character ('/0').

char mainstring[] = "This is a main string";
Code:
for (int i=2; i<=7; ++i)
{
   cout << mainstring[i] << endl;
}
That will print out characters from 2 to 7 (from a zero based index).
__________________
my site
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting a massive multidimensional array dave111 PHP 1 12-12-05 04:09 PM
Serializing a set of arrays dannyallen PHP 2 10-11-04 03:04 AM
simple array question jimcart JavaScript 1 03-19-04 05:38 PM
linking to iframe not working :( j0d JavaScript 5 01-19-04 08:14 PM


All times are GMT -5. The time now is 04:35 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.