Thread: student needs help with C strings

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    9

    student needs help with C strings

    alright, i need help on a project in class. Im new with C so be gentle
    i have a series of statically assigned character arrays that i need to append and prepend. So heres the scenario:

    str1 [17]

    str1 takes user input in the form of three characters at a time in a loop, i need to:
    a) append the three characters to the end of the string, when the length goes beyond 17 it needs to start dropping characters off the begining.
    b) prepend the three characters to the beginning of the string, when the length goes beyong 17 characters it needs to start dropping characters off the the end of the string.

    ive been using the strcat() function for the appending, but cant figure out how to make it drop chars off the beginning. Also, is there a similar function for prepending?

    all of this is being written on an XP box with SP2, and then being cross compiled to run on a motorola 68hc11 microcontroller.

  2. #2
    Unregistered User
    Join Date
    Sep 2005
    Location
    Antarctica
    Posts
    341
    If you have a string and you want to chop off the first 3 chars and add 3 chars to the end of it, you can do something like this:

    Code:
    char myBuffer[17];
    ... fill it ...
    char myThreeChars[4]; (4 because you want an extra char for the NULL terminator).
    ... read your 3 chars ...
    
    sprintf(myBuffer, "%s%s", &myBuffer[3], myThreeChars);
    to start at the 3rd char in the string, you just pass the address of the 3rd item in the array.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    There's no such function, but you just need to understand how C "strings" work. They are simply null terminated character arrays. If you want to drop three characters off the beginning of an array of chars, you'd need to move the 4th character to position 1, the 5th character to position 2, and so on. There are a variety of ways to accomplish this. If you have trouble, post your code so far and we'll have a look.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    9
    thanks guys...i sort of have it working

    heres the apend part that i have

    Code:
    		if (switchvalue == 1){
    		sprintf(str1,"keys %c%c%c Op:%u",char1, char2, char3, switchvalue);
    		sprintf(temp,"%c%c%c",char1, char2, char3);
    		if (strlen(output)>=15){
    			output[0] = output[3];
    			output[1] = output[4];
    			output[2] = output[5];
    			output[3] = output[6];
    			output[4] = output[7];
    			output[5] = output[8];
    			output[6] = output[9];
    			output[7] = output[10];
    			output[8] = output[11];
    			output[9] = output[12];
    			output[10] = output[13];
    			output[11] = output[14];
    			output[12] = output[15];
    			output[13] = output[16];
    			output[14] = char1;
    			output[15] = char2;
    			output[16] = char3;
    
    		}
    
    		strcat(output,temp);
    
    
    		VTlcd_print(0,0,str1,0);
    		VTlcd_print(1,0,clear,0);
    		VTlcd_print(1,0,output,0);
    		}
    the problem with what i have now is that since the characters are inputed 3 at a time, so in order for that big if statment to work, the strlen function must be a multiple of 3, or it will either skip it once (if i make it >= 16) or chop off the last char of the string (which is what its doing now.) However, i will try using just sprintf as suggested above. I sort of tried that before but the little LCD display i have hooked up to the 68hc11 wass displaying all sorts of disturbing stuff.

    thanks guys...this is my first thread here and im sure ill be back, as this class for all intents and purposes is "teach yourself C"

  5. #5
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Use a loop instead of individually assigning each character. That way you can more easily cope with having to prepend < 3 characters.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updating in a sequential file?
    By Ronnyv1 in forum C Programming
    Replies: 1
    Last Post: 03-24-2009, 04:41 PM
  2. Database assignment is Killing me!
    By Boltrig in forum C Programming
    Replies: 2
    Last Post: 11-29-2007, 03:56 AM
  3. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  4. LinkList Sorting in C
    By simly01 in forum C Programming
    Replies: 3
    Last Post: 11-25-2002, 01:21 PM
  5. Creating a student grade book-how?
    By Hopelessly confused in forum C Programming
    Replies: 5
    Last Post: 10-03-2002, 08:43 PM