Help with String Appendage

This is a discussion on Help with String Appendage within the C Programming forums, part of the General Programming Boards category; Hi I'm having trouble with some code...I'm getting all kinds of nasty errors and can't figure out how to fix ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    8

    Help with String Appendage

    Hi I'm having trouble with some code...I'm getting all kinds of nasty errors and can't figure out how to fix them. this program takes one string and adds it onto the end of another.

    Code:
    #include <stdio.h>
     
    int my_strlen( char s[]) 
    {
      int i = s[0];
     
      while( s[i] != '0')
        ++i;
     
      return i;
    }
     
    
     
    char * my_strappend( char s1[], char s2[])
    {
      int i, j;
     
      for( i = 0, j = strlen(s2); s1[i] != 0; ++i, ++j)
        s2[j] = s1[i];
     
      s2[j] = s1[i];
     
      return s2;
    }
     
    int main( void)
    {
        
      char name="buf"; 
     
      char buf[100] = { 'a', 'b', 'c' }; 
     
      printf( "my_strlen(buf) = %d\n", my_strlen(buf) );
     
      my_strappend( buf, name); 
     
      printf( "name = %s, buf = %s\n", name, buf);
     
      return 0;
    }
    Any help would be great, thank you!

  2. #2
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Code:
    int i = s[0];
    I think you mean
    Code:
    int i = 0;
    Your code is setting the index variable to whatever value is the first character in the string.
    Code:
    while( s[i] != '0')
    I think you mean
    Code:
    while ( s[i] != '\0' )
    '0' is different from '\0'.
    Code:
    char name="buf";
    I think you mean
    Code:
    char *name = "buf";
    Code:
    char buf[100] = { 'a', 'b', 'c' };
    I think you mean
    Code:
    char buf[100] = "abc";
    An array of char isn't a string until you put '\0' at the end of it.
    Code:
    my_strappend( buf, name);
    I think you mean
    Code:
    my_strappend( name, buf );
    my_strappend() appends the first string onto the second string, not the other way around.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    8
    Thank you for all your help! I still have one question, however. I keep getting an error at line 19

    19 `strlen' undeclared (first use this function)

    Isn't strlen an inherent c function, or does it have to be declared like any other function? I'm pretty sure it's built in, but it gives me that error...grrr

  4. #4
    Registered User Noir's Avatar
    Join Date
    Mar 2007
    Posts
    218
    Isn't strlen an inherent c function, or does it have to be declared like any other function?
    Every function has to be declared first. You can do it yourself if you want, but with standard functions it's like hitting a moving target. That's why we have standard headers. If you include <string.h>, that error will go away because <string.h> declares strlen().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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