croping strings

This is a discussion on croping strings within the C Programming forums, part of the General Programming Boards category; how could i have my program cut off the begining of my string? Ex.... str[]="abcdefg" ?????(str,3/*number of leters to crop*/) ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,498
    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, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07: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, 10: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

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