setting a string to a part of an array?

This is a discussion on setting a string to a part of an array? within the C++ Programming forums, part of the General Programming Boards category; if i want to set a string from the second index in an array until a '

Thread: setting a string to a part of an array?

' character how would ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    137

    setting a string to a part of an array?

    if i want to set a string from the second index in an array until a '\0' character how would i do that?

    i can't str_cpy() can i?

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    for-loop. starts at the required index value. terminates when the char being examined is equal to '\0'. inside the loop you assign the char from the array to the proper index position in your string, e.g. mystring[i-1] = arr[i];
    Last edited by 7stud; 02-04-2006 at 07:34 PM.

  3. #3
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Maybe a while loop:
    Code:
    int I = 1;
    while( charArray[I] != '\0' )
    {
      theString += charArray[I];
      ++I;
    }
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    Yes, you can use strcpy. Assuming you are using C style strings, just use strcpy(target, source + 1) or replace 1 with whatever index you want to start with.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    137
    i just used a while loop. i figured that was probably the fastest way, but it seems kind of slow. hopefully its not as i use it a lot. thank you for the help.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    If you implemented it correctly it shouldn't be any slower than using strcpy, but why not use the built-in function anyway?

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You would have to include the whole <cstring> header file?

  8. #8
    Registered User
    Join Date
    Nov 2002
    Posts
    126
    If you're really concerned about speed, and you don't mind pointer arithmetic, behold the fastest way:

    Code:
    #include <iostream>
    
    int main()
    {
    	char foo[] = "BlahThis is a test\0";
    	cout <<(foo + 4) <<endl; //Outputs "This is a test"
    }

  9. #9
    Registered User
    Join Date
    Feb 2005
    Posts
    59
    Do you really need the '\0'? I thought the compiler automatically inserted the null terminator to the end of string literals.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 11:09 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21