Thread: Question about Strings

  1. #1
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427

    Question about Strings

    if I declare a
    char ArrayOfChar[50];

    And then I do

    printf("Enter your Name");
    //the user types in John Doe

    gets(ArrayOfChar);


    This would only be using 9 chars right (one extra one for the null character "\0").....so what happens to the other 41 bytes reserved for chars in this array? Does the computer fill it up with something?

    Also how would you set up a for loop to copy everything from one string to the other until it encounter a null character?

    this is what I've though of so far

    for (int i=0; SourceString[i] !='\0', i++)
    DestinationString[i]=SourceString[i];


    Please your input on this........

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    dont use gets()
    use fgets()

    the other 41 chars are assorted garbage that was in there before as the array is uninitialised.

    why bother? its already done for you. look up strcpy() in your help files.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    I am writing a small program and yes I used fgets

    I want to know this because this is what I want to do.....

    I am going to ask the Name of a Baseball Player for instance right.........then I am going to create some sort of profile on him with his information, and I want to safe the file as his name......

    so if the user types in Babe Ruth........

    I want to append .txt to the end of the string so it can be

    Babe Ruth.txt.........

    I tried doing this........but apparently it doesn't work....

    Code:
    int StringLength;
    StringLength=strlen (NameOfFile);
    printf("%d", StringLength);
    NameOfFile[StringLength];
    NameOfFile[StringLength+1]='.';
    NameOfFile[StringLength+2]='t';
    NameOfFile[StringLength+3]='x';
    NameOfFile[StringLength+4]='t';
    
     Where NameOfFile is "Babe Ruth" or something 
    PS Yeah I am a newbie I am just practicing some concepts of C......because I just almost finished reading a book on it.
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  4. #4
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Probaly it won't work because fgets() leave an '\n' at the end of your string, remove it and use strcat() to add ".txt" to the file.

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    You forgot to terminate the string with a '\0'. Also, NameOfFile[StringLength] == '\0', so you want to start your '.' there.

    gg

  6. #6
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Maybe you should try something like that:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define EXTENSION ".txt" //the extension we'll add to the file name
    int main(void)
    {
        char fileName[100]; //the file name we'll get
        
        //get the file name
        printf("Please, enter the file name, without extension\n");
        fgets(fileName,sizeof(fileName),stdin);
        
        //remove \n from the string, and add .txt
        fileName[strlen(fileName)-1] = '\0';
        strcat(fileName,EXTENSION);
        
        //display result
        printf("The full file name: %s\n",fileName);
        system("PAUSE");
        return 0;
    }

  7. #7
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Thank you people.........I didn't know there was a function as "strcat" :-D
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >fileName[strlen(fileName)-1] = '\0';
    Don't forget to test that strlen(fileName)-1 actually is a newline. Otherwise you would be trashing a valid character in the string.

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

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    yes Prelude I know that, but I have a question, if in the end of the string we have null, and in strlen-1 we put another null (because we changed the \n to null) this wont give any problem? Whats the better way to remove the \n from the string?

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Vber
    yes Prelude I know that, but I have a question, if in the end of the string we have null, and in strlen-1 we put another null (because we changed the \n to null) this wont give any problem? Whats the better way to remove the \n from the string?
    No, it won't give a problem. There are various ways to do this, we had a thread about this sometime ago, I'll see if I can find it.

    In the meantime:

    Code:
    char *p;
    p = strchr(str, '\n');
    if (p)  *p = '\0';
    [edit]
    Here's the earlier thread on this:
    http://cboard.cprogramming.com/showt...threadid=24604
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Wow! Thanks a lot Hammer and Prelude.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about strings in c
    By gp364481 in forum C Programming
    Replies: 9
    Last Post: 11-13-2008, 06:32 PM
  2. Question About Strings
    By spanker in forum C++ Programming
    Replies: 1
    Last Post: 07-13-2008, 05:09 AM
  3. strings question
    By cstudent in forum C Programming
    Replies: 4
    Last Post: 04-18-2008, 07:28 AM
  4. Functions and Strings Question
    By StrikeMech in forum C Programming
    Replies: 4
    Last Post: 07-18-2007, 06:07 AM
  5. Strings question
    By kimimaro in forum C Programming
    Replies: 10
    Last Post: 03-15-2005, 12:14 AM