whats wrong with this function?

This is a discussion on whats wrong with this function? within the C++ Programming forums, part of the General Programming Boards category; I just made this function a miniute ago, and its suppost to remove the space off of the beginning of ...

  1. #1
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428

    whats wrong with this function?

    I just made this function a miniute ago, and its suppost to remove the space off of the beginning of a string. It compiles fine, but doesn't seem to do anything at all to the string. Im not sure why its not working. Thanks! Also, I was searching the forum for a possiable answer to this before, and one of the posts made a reference to 'isspace()'. What header does that function lie in?

    Code:
    //in main:
    command[256]=a_remove_space(command);
    cout << command;
    
    //the function
    char a_remove_space(char string[])
    {
    	char newstring[256];
    	int i2=0;  //used in local embedded loop
    	int last_slot=0;  //the slot of the string that a space is no longer found
    	bool done=false;
    	
    	
    	
    	//remove spaces from string
    	for(i=0;i<(strlen(newstring));i++)
    	{
    		if(string[i]!='/0')  //if we found a slot without a space then continue
    		{
    			last_slot=i;  //get the slot of the string that we found the letter
    			for(i2=last_slot;i2<(strlen(string));i2++)  //initialize a new loop to re-fill the array
    			{
    				newstring[i2-last_slot]=string[i2];  //refill the array
    				done=true;
    			}
    		}
    		if(done==true) break;  //exit the base for...loop if done is set to true
    	}
    
    	return newstring[256];
    }
    In main, it prints the string just as it was before, with the spaces in the front. Thanks for any help!

  2. #2
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    by beginning I mean all the characters from the string, starting from '0', that are spaces. If the string was ' hello' Then, since there are 7 spaces, I would want to shave the 7 spaces off.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    12
    do you want it to remove the empty space only when outputting??

  4. #4
    Registered User dirkduck's Avatar
    Join Date
    Aug 2001
    Posts
    428
    "If the string was ' hello' Then, since there are 7 spaces"

    This forums took out the spaces, there should be 7 spaces before the text hello.

    Kespire - Not only when outputting, but the actual character array, as later it is used with other string manipulaton functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  3. Replies: 3
    Last Post: 03-04-2005, 01:46 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 06:18 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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