String Processing

This is a discussion on String Processing within the C++ Programming forums, part of the General Programming Boards category; Hi all, I was wondering if anybody knew the source code of the strncat() function. I have tried for the ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Question String Processing

    Hi all,
    I was wondering if anybody knew the source code of the strncat() function. I have tried for the past five hours and I have had no luck. Maybe its the environment that I amusing... whatever it is can you please help me out?

    basically what the strncat() function does is it takes two strings, str1 and str2 and concatenates str2 to the end of str1 but only till the number of chars we specify. For example

    if the function call was strncat("wow", "awesome", 3) the output would be "wowawe"
    Please let me know what I am doing wrong thank you.

  2. #2
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Here's the code..sorry

    forgot to place my code...sorry


    char *strncat(char *str1, const char *str2, size_t count)
    {
    char *tmp = str1;
    while (*tmp++);
    tmp = tmp--;
    while (*str2)
    *tmp++ = *str2++;
    *tmp = *str2;
    return (str1);
    }
    }

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Each implementation of the standard functions may be and often are different. The Standard only specifies how it works for the user, not how it is implemented, but here is a possible source:
    Code:
    char *strncat ( char *s1, const char *s2, size_t n )
    {
      char *s;
      for ( s = s1; *s != '\0'; s++ );
      for ( ; n > 0 && *s2 != '\0'; n-- )
        *s++ = *s2++;
      *s = '\0';
      return s1;
    }
    >if the function call was strncat("wow", "awesome", 3) the output would be "wowawe"
    This is incorrect, "wow" is a string literal and may be in read-only memory, so you can't modify it or access memory that you do not own. A proper call to strncat would be:

    char a[7] = "wow";
    strncat ( a, "awesome", 3 );

    -Prelude
    My best code is written with the delete key.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    92

    Question one more function

    I just wanted to ask you about one more function everyone and then I can finally go to sleep tonight. I was wondering if anybody knows how to implement the strrchr(). Again, for those people not familar with this function, it returns a pointer to the last occurrence of a string. So for example,

    strrchr(this is a test, 't') = test

    Can someone put my tortured soul at ease? Thank you.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    char *strrchr ( const char *s, int c )
    {
      const char ch = c;
      const char *sc;
      for ( sc = NULL; ; s++ ) {
        if ( *s == ch )
          sc = s;
        if ( *s == '\0' )
          return (char *)sc;
      }
    }
    Once again your call to the function is incorrect, it should be:

    test = strrchr ( "this is a test", 't' );
    // Work with test.

    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    92
    Thanks Again Prelude!

  7. #7
    Registered User harryP's Avatar
    Join Date
    Sep 2002
    Posts
    124
    Okay this is making me mad. I'm making my own strncat() function called StrComb(). It does the same thing, but like with StrComp(), I like doing it my own way. However, this just isn't working. Here's my code. Could someone please help me to find where I strayed off? It copies the first string to a new one just fine, but then it has issues appending to it and adding the second:
    Code:
    char * StrComb(char * str1, char * str2, int size)
    {
    	int i;
    	int pos;
    	char * result = new char;
    
    	// first enter in the 1st string
    	for (i = 0; i < GetStrSize(str1); i++)
    		result[i] = str1[i];
    
    	pos = i + 1;
    
    	for(i = 0; i < size; i++)
    		result[pos] = str2[i];
    	// return the result
    	return result;
    }
    Thanks!

    Brendan
    Draco dormiens nunquam titallandus.
    Console Graphics Library: http://www.geocities.com/steve_alberto/cgl.html

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,703
    First off, you allocated a single char and then try to copy an array of chars to it! Memory corruption!

    And this does nothing since pos isn't incremented:

    for(i = 0; i < size; i++)
    result[pos] = str2[i];


    So if you really want to allocate new memory, declare it like:

    char * result = new char[GetLength(str1) + GetLength(str2) + 1];

    Next, keep in mind that you will have to deallocate the memory on th calling side every time you use this function:

    char *s = StrComb("The Cat ", "in the Hat", 20);
    //...use it...
    delete[] s;

    Also, if returning new memory, why use the third parameter at all?

    The standard strncat copies the second string to the first, exceeding no more than string one's total size, then returns a pointer to the first element of string one. Keeping that in mind we have:


    Code:
    
    char * StrComb(char * str1, char * str2, int size)
    {
      int i = 0, k = 0, length = GetStringLengh( str2);
    
      while( str[i] != 0 ) i++; //...reach null terminator.
    
      while( i < size && k < length)
       {
         str1[i++] = str2[k++];
       } 
    
     str1[i] = 0; //...null term.
    
     return str1; //...return a pointer to str1. 
    }
    Code:
    bool flip(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() < 0;
    }

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. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. segmentation fault when processing a string
    By EMC2 in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2003, 02:56 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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