char array help needed

This is a discussion on char array help needed within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a code segment below I use to remove certain characters from an array of characters. I would ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    59

    char array help needed

    Hello,

    I have a code segment below I use to remove certain characters from an array of characters.

    I would like to:

    1) remove the first two characters
    2) keep the middle characters up to either the '.' character or the '_' character.

    For instance:

    "alwater_0.shp" would become "water".
    "alnetw.shp" would become "netw".

    My problem is my code only seems to perform 2) and not 1).

    I don't know how to do 1) and then perform 2).

    My code is below. Again, 2) works but 1) does not:

    Code:
    //stripLayer function
    void stripLayer(char * n, char * o)
    {
    	 //1) skip the first two characters
    	*n = *(o+2);	//set the pointer in a to the third character of b
    	
    	//2) eliminate the rest of the characters from either the _ or the . char
    	for( ; (*o != '_') && (*o != '.'); o++, n++) 
    	{ *n = *o; }
    
    	//place a null at the present character
    	*n = '\0';
    
    	return;
    
    }	//end stripLayer
    Last edited by VanJay011379; 07-21-2002 at 03:59 PM.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    There is a standard library function that is used to extract substrings, and there is also one used to find characters in a string. Search the string handling library for the appropriate methods.

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    I just need to know how to skip to the third character in the array before I enter the for loop.
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  4. #4
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> I just need to know how to skip to the third character in the array before I enter the for loop.

    But you are already doing that :

    *n = *(o+2);//set the pointer in a to the third character of b

  5. #5
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Originally posted by VanJay011379
    I just need to know how to skip to the third character in the array before I enter the for loop.
    Use the substring method to do that. Start the substring extraction at the third character. I think the method is something like 'substr(start, end)'. It might be a method of 'string' type.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    59
    Thanks to your suggestions I figured it out

    Code:
    //stripLayer function
    void stripLayer(char * n, char * o)
    {
    	//move to the second character of o
    	char * o2;
    	o2 = o + 2;
    
    	//eliminate the rest of the characters from either the _ or the . char
    	for( ; (*o2 != '_') && (*o2 != '.'); o2++, n++) 
    	{ *n = *o2; }
    
    	//place a null at the present character
    	*n = '\0';
    
    	return;
    
    }	//end stripLayer
    " . . . and I lay awake, big dreamers never sleep." - David Lee Roth

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    95

    error cheking

    You may want to include some type of error checking to insure the stirng is at least 3 char long.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 08:39 AM
  2. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  3. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  4. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 09:21 AM

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