replacing string elements

This is a discussion on replacing string elements within the C Programming forums, part of the General Programming Boards category; Hello, I am student trying to explore simple 'L-Systems' in a simple C program.. apparently not simple enough for me. ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    replacing string elements

    Hello,

    I am student trying to explore simple 'L-Systems' in a simple C program.. apparently not simple enough for me.

    I would like to write a program which looks at a string and replaces parts of it conditionally, through a number of trials.

    * Pseudo-
    Code:
    starting with  string[0] = 'a'
    
    for NUMTRIALS  i++
    
    	for STRINGLEN   j++
    
    	    if( string[j]  ==  'a' )
    	        string[j]  = 'b' 	
    	
    	    else if (  string[j]  ==  'b' )
    	       string[j]  = 'ab'
    Output to the effect of:
    i = 0 string = 'a'
    i = 1 string = 'b'
    i = 2 string = 'ab'
    i = 3 string = 'bab'
    i = 4 string = 'abbab'

    * thought it better to write it so it could be understood despite my incompetence.

    I have tried to do this with character strings and boggled my mind.
    It would be fine to do it with integers and then convert to letters.
    Any suggestions for how to represent this (types, procedure, existing lib functions!) would be appreciated.

    Thank you for reading this, and any help.

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This part will be tricky:
    Code:
    	    else if (  string[j]  ==  'b' )
    	       string[j]  = 'ab'
    Since string[j] would be one character, not two, so in C you would need to copy the string up to j into another (empty) string, add 'a' and 'b', then add the rest of the string, and I suppose copy it back into the original string if that string has enough space available.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Can you make the above code in C, even if it doesn't work or compile?? There is a homework policy in this forum so it is generally asked to demonstrate what you have tried in a give language.

    A string in C is an array of characters that are terminated with the '\0' character. Here is an example
    Code:
    char string[1024]; //allocate a big array of characters
    string[0] = 'a';       //the first character is 'a'
    string[1] = 'b';       //the second character is 'b'
    string[2] = '\0'       //you terminate the string
    You terminate the string so you know where to stop. If you try
    Code:
    printf("%s", string);
    you will see that it prints "as".

    For your program think of it as an array of elements. As MK27 said you cannot just write 'ab' on the string. You have to write one by one the characters. For example, you would do
    Code:
        else if (  string[j]  ==  'b' )
    	       string[j]  = 'a'
                    j = j + 1         //go to the next character
                    string[j] = 'b'
    The
    Code:
    for STRINGLEN   j++
    part would be
    Code:
    for (j = 0; string[j] != '\0'; ++j)
    so you start from the beginning. If you don't find the terminating character, you continue

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    2
    Think this is working as intended. Makes sense too.
    Thank you both very much. 'Homework policy' is a good thing.

    Code:
    			else if (  string[j]  ==  'b' ){
    			   
    			   LEN = strlen(string);
    			   
    			   for( k = 0 ; k < j ; k++)  			// copy thusfar into head 
    					head[k] = string[k];
    
    			   for( k = j+1, l = 0 ; k < LEN ; k++, l++)  // copy hereafter into tail 
    					tail[l] = string[k];
    
    		   	   head[j]  = 'a' ;         			// add in replacement 1
    			   head[j+1]  = 'b';				// add in replacement 2
    
    			   LEN++;
    			   for( k = 0 ; k < LEN ; k++)     	        // copy head (+2) back to string
    				string[k] = head[k];
    
    			   for( k = j+2, l = 0 ; k < LEN ; k++, l++)  // copy tail back back to string
    					string[k] = tail[l] ;
    
    				string[LEN]  = '\0';		         // terminate at incremented position		
    				j++;                                          // increment counter		 		
    			}

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Make sure you do not overwrite the bounds of the string!
    Code:
    char string[] = "hello world";
    This string is 12 bytes long, 11 characters + '\0'. If I run that code snippet on it and replace 'l' with "ab", it will now be 14 characters! VERY BAD! That will overwrite some memory and possibly cause a crash.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

Tags for this Thread


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