Results 1 to 4 of 4

Thread: I need help with strings in C

Hybrid View

  1. #1
    Guest

    I need help with strings in C

    Yes, in C. *Don't flame the Ruby guy just yet please. *Here are a couple of things I would like to know/understand:
    * How to input a string in the safest possible way (the input might have space characters in it (like when you ask for a name) so make sure the code works with that)
    * How to safely concatenate 2 strings
    * How to safely copy a string
    * How to remove the Enter ("\n&quot from a string (chomp in Ruby and Perl)

    A note to Ashcrow: I do not have strlcat and strlcpy (though I wish I did), so don't give me example using these.

  2. #2
    Guest

    Re: I need help with strings in C

    For concatenation, copying and comparison, couldn't you use strcat(), strcpy and strcmp()?
    (Did you misspell?)

    To chomp, why not do exactly what the strcpy function does, but instead of exitting when the null char is reached, exit the loop when the newline is reached and then stick a null char at the end of the copied string (it depends how you do it though).

    Code:
    while (*src != '\n')
     * *dest++ = *src++;
    
    *dest = '\0';
    ...and for the space thing, do you mean in 1 string contain all spaces or split text into strings delimited by spaces?
    If it's one big string, I'm not sure...
    If it's splitted, well I think you'd know how to do that....so yeah I don't know either.

  3. #3
    Guest

    Re: I need help with strings in C

    first of all, you have to know that in C a string is just an array of characters, followed by a \0 character (very important)

    * How to input a string in the safest possible way (the input might have space characters in it (like when you ask for a name) so make sure the code works with that)
    char buffer[100]; */ always use the lenght u want + 1. the extra 1 is for the \0 */
    fgets(buffer,100,stdin);
    buffer[strlen(buffer) - 1] = '\0'; */ this replaces the \n by \0 */
    * How to safely concatenate 2 strings
    strcat(targetstring, sourcestring);
    * How to safely copy a string
    strcpy(targetstring, sourcestring);
    * How to remove the Enter ("\n&quot from a string (chomp in Ruby and Perl)
    buffer[strlen(buffer) - 1] = '\0';


  4. #4

    Re: I need help with strings in C


    Code:
    char buffer[100]; */ always use the lenght u want + 1. the extra 1 is for the \0 */
    fgets(buffer,100,stdin);
    buffer[strlen(buffer) - 1] = '\0'; */ this replaces the \n by \0 */
    the probelm is a \0 is placed at buffer+101
    you want to make it only read 99 charectors just in case
    Code:
    strcat(targetstring, sourcestring);
    strcpy(targetstring, sourcestring);
    tisnt safe,
    strncat(targetstring, sourcestring, AMMOUNTOFCHARSTOCOPY);
    strncpy(targetstring, sourcestring, SIZEOFTARGETSTRING);

    ha when i was writting this i started to do <esc>:wq

    :wq

Similar Threads

  1. Perl and Strings
    By paradox in forum Linux - Software, Applications & Programming
    Replies: 5
    Last Post: 08-04-2004, 08:28 PM
  2. strings command
    By Emooz in forum Linux - General Topics
    Replies: 1
    Last Post: 07-13-2004, 10:30 AM
  3. strings for window?
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 1
    Last Post: 05-08-2004, 01:42 AM
  4. C++ strings error
    By Blaqb0x in forum Linux - Software, Applications & Programming
    Replies: 6
    Last Post: 05-23-2002, 03:59 PM
  5. Adding strings in FreeBSD startup??
    By Kernel_Killer in forum BSD
    Replies: 10
    Last Post: 03-24-2002, 02:13 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •