Thread: replace character with string

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    replace character with string

    I need some help on writing code for the fill in the blank question type.

    I would like the proccess to end with these results:
    question:"C *"
    answer:"Board"

    question after * is replaced."C -----"

    question:"Yet * question"
    answer:"another"

    question after * is replaced."Yet ------- question"

    strtok and/or strchr are two functions that I'll probabley need, but that's all I know. Any hints?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  2. #2
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You simply move to the end of the original string back up two, one for the \0 and the other for *. Then just write the rest of the string and terminate it.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Ok, I have made an attempt to use your advice to solve my problem.

    Posted below is my code. What improvements I can make to it?

    Code:
    	 char input[80] = "January comes after * one two three.";
    	 char input2[80] = "Feburary";
    	 char replace[80];
    	 char temp[80];
    	 char *p;
    	 int i = 0;
    	 /* strtok places a NULL terminator
    	 in front of the token, if found */
    	 p = strtok(input, "*");
    	 if (p)   printf("%s\n", p);
    	 strcpy(temp,p);
    	 for(i = 0;i < strlen(input2);i++)
    		replace[i] = '_';
    	 strcat(temp,replace);
    	 printf("%s\n",replace);
    	 /* A second call to strtok using a NULL
    	 as the first parameter returns a pointer
    	 to the character following the token  */
    	 p = strtok(NULL, "*");
    	 strcat(temp,p);
    	 if (p)   printf("%s\n", p);
    	 strcpy(input,temp);
    	 printf("%s\n",input);
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  4. #4
    Master of the Universe! velius's Avatar
    Join Date
    Sep 2003
    Posts
    219
    You are working with too many arrays of chars. I'd drop it down to to. Then just tidy up your code.
    While you're breakin' down my back n'
    I been rackin' out my brain
    It don't matter how we make it
    'Cause it always ends the same
    You can push it for more mileage
    But your flaps r' wearin' thin
    And I could sleep on it 'til mornin'
    But this nightmare never ends
    Don't forget to call my lawyers
    With ridiculous demands
    An you can take the pity so far
    But it's more than I can stand
    'Cause this couchtrip's gettin' older
    Tell me how long has it been
    'Cause 5 years is forever
    An you haven't grown up yet
    -- You Could Be Mine - Guns N' Roses

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Sorry, but I don't see how you would use only two char arrays for that. Could you please explain?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    161
    Here is something that I threw together really quick.

    Code:
    #include <string.h>
    #include <stdlib.h>
    #include <stdio.h>
    
    void add_blanks(char *question, char *answer, char* res)
    {
      int alen = strlen(answer);
      int i;
      char* p;
      char* qp;
    
      strcpy(res, question);
    
      p = strstr(res, "*");
      if(!p) return;
     
      for(i=0; i<alen; i++, p++) *p = '-';
    
      *p = ' ';
      p++;
    
      qp = strstr(question, "*") + 2;
      strcpy(p, qp);
    }
    
    
    int main()
    {
      char question[] = "The month before * is January.";
      char answer[] = "February";
    
      char *res = malloc(strlen(question) + strlen(answer));
    
      add_blanks(question, answer, res);
    
      printf("%s\n", res);
    
      free(res);
      return 0;
    }

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >Sorry, but I don't see how you would use only two char arrays for that.

    Yet another way.
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *foo(char *question, size_t size, const char *answer)
    {
       size_t alen = strlen(answer);
       if ( alen + strlen(question) < size )
       {
          char *star = strchr(question, '*');
          if ( star != NULL )
          {
             memmove(star + alen, star + 1, strlen(star));
             memset(star, '-', alen);
             return question;
          }
       }
       return NULL;
    }
    
    int main()
    {
       char question[80] = "The month before * is January.",  answer[] = "February";
       if ( foo(question, sizeof question, answer) != NULL )
       {
          puts(question);
       }
       return 0;
    }
    
    /* my output
    The month before -------- is January.
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Thanks for the better solutions to this problem. I understand most of thefroggy's code, but I have problem with how memmove and memset work in Dave_Sinkula's solution. Could you tell me how they work in your in code?
    Don't you dare hit me on the head, you know I'm not normal.
    A Stooge Site
    Green Frog Software

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    >memmove(star + alen, star + 1, strlen(star));
    void *memmove(void *dest, const void *src, size_t n);

    The memmove() function copies n bytes from memory area src to memory area dest. The memory areas may overlap.
    We have 'star' pointing to the asterisk. strlen(star) is the length of the remaining string, from the '*' up to, but not including, the '\0'. So the 'src' parameter is pointing to the character just after the '*'. And the value of 'alen' is the length of the answer string. Finally, 'dst' points to a location ahead of the '*' by 'alen', the length of the answer string.

    So, we use memmove to copy the remaining string from the position after the asterisk to a position beyond the asterisk by the length of the answer string.

    >memset(star, '-', alen);
    void *memset (void *s, int c, size_t n);

    The memset() function fills the first n bytes of the memory area pointed to by s with the constant byte c.
    Starting at the asterisk, it fills the string -- the "hole" we skipped in the memmove call -- with as many '-' as there are characters in 'answer'.
    Last edited by Dave_Sinkula; 10-18-2003 at 03:51 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 46
    Last Post: 08-24-2007, 04:52 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM