Thread: croping strings

  1. #1
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125

    croping strings

    how could i have my program cut off the begining of my string?

    Ex....
    str[]="abcdefg"
    ?????(str,3/*number of leters to crop*/)
    <str="defg">
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  2. #2
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    i think you can use strcat - look it up in you compiler help
    Monday - what a way to spend a seventh of your life

  3. #3
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125
    no....my compiler refrence says it appends one string to the end of another
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  4. #4
    Hamster without a wheel iain's Avatar
    Join Date
    Aug 2001
    Posts
    1,385
    try reading the the characters into another string, deleting the original and then copying it in. I know its ineffcient but cant think of another way at the moment
    Monday - what a way to spend a seventh of your life

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Here's a possible solution:

    Code:
    #include <stdio.h>
    
    void CropString(char *p, int amt);
    
    int main()
    {
    	char str[] = "123456789";
    
    	CropString(str, 3);
    	
    	printf( "str: %s\n", str );
    	
    	return 0;
    }
    
    void CropString(char *p, int amt)
    {
    	char *t;
    
    	t = p;		// used to walk through the string
    
    	while (*t)	// while we are not at the end of the string
    	{
    		if (amt)	// skip over characters
    		{
    			amt--;
    			t++;
    		}
    		else		// start copying characters
    		{
    			*p = *t;
    			p++;
    			t++;
    		}
    	}
    
    	*p = '\0';		// finally, add the null
    }
    clu82

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if you want to save some copying, do this

    Code:
    str[]="abcdefg" 
    char *clipped_str = &str[3];
    printf( "%s\n", clipped_str );

  7. #7
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125
    Thank You so much! It works perfectly now!
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Problems with strings as key in STL maps
    By all_names_taken in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:34 AM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM