Thread: Converting string to char array

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    13

    Converting string to char array

    Hi, I have another problem

    I've written a program which reads in a text file into a string.

    Now, I want to convert that entire string so each char is held seperately in an array.

    Basically I'm not sure how to pluck each character out of the string and store it seperately?

    Code:
    /*Program to read in text file and store data in a string*/
    
    #include<stdio.h>
    
    void main()
    {
    	/*declare and initialise variable*/
    	char message[400];
    	int i=0;
    
    	FILE *file_in;
    	file_in=fopen("text.txt", "r");
    
    	/*stores and prints the data from the string*/
    	fgets (message , 400 , file_in);
        puts (message);
    
    }
    Last edited by frankchester; 11-30-2010 at 06:20 PM.

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    But your string already is an array of char.

    If you want to access each char, you just have to look at message[0], message[1], up until you find a zero (which marks the end of a string). If you want to copy a string, you can use strcpy() (make sure the target buffer is large enough).

    What is your ultimate goal?

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by cas View Post
    But your string already is an array of char.

    If you want to access each char, you just have to look at message[0], message[1], up until you find a zero (which marks the end of a string). If you want to copy a string, you can use strcpy() (make sure the target buffer is large enough).

    What is your ultimate goal?
    The next step of this program is to encrypt it using a caesar cipher, thus I figured I'd need to have each char as separated as possible so that I cna encrypt each separately. Will I be able to encrypt just by selecting which one by its number?

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by frankchester View Post
    The next step of this program is to encrypt it using a caesar cipher, thus I figured I'd need to have each char as separated as possible so that I cna encrypt each separately. Will I be able to encrypt just by selecting which one by its number?
    That's going to depend how you write your code... but Yes that is one way of doing it.

    By way of agreeing with the others, C doesn't know strings. In C a string is simply an array of char that some library functions treat as a string.

    You have declared char message[400]; that sure looks like an array to me

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    By way of agreeing with the others, C doesn't know strings. In C a string is simply an array of char that some library functions treat as a string.
    You and everyone you are agreeing with are wrong. The standard implicitly defines what a string is.

    "A string is a contiguous sequence of characters terminated by and including the first null character. The term multibyte string is sometimes used instead to emphasize special processing given to multibyte characters contained in the string or to avoid confusion with a wide string. A pointer to a string is a pointer to its initial (lowest addressed) character. The length of a string is the number of characters preceeding the null characters and the value of a string is the sequence of the values of the contained characters, in order."


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Location
    xian china
    Posts
    31
    You have stored the text in the array, you can use it directly.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    You and everyone you are agreeing with are wrong. The standard implicitly defines what a string is.

    "A string is a contiguous sequence of characters terminated by and including the first null character.
    And how is that stored... in an array.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    So, by definition, C knows strings.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547

    Thumbs up

    Quote Originally Posted by whiteflags View Post
    So, by definition, C knows strings.
    No, Pascal knows strings... lots of other languages know strings... they have data types for just the purpose and can manipulate them natively.

    Code:
    // pascal version
    Program Hello;
    
    var 
    string s;
    string c;
    
    begin
       s := "hello";
       c := s;
       s := " world";
      
      write ( c );     // output = hello world
      write ( s );  
    
    end.
    
    
    // C version
    #include stdio.h
    
    char s[10];
    char c[10];
    
    int main(void)
      { strcpy(s,"hello");
         c = s;
         strcpy(s,"world");
    
         puts( c );    // output  = world world
         puts( s );    // and a memory leak
      }
    C has no native string awareness.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    So "hello" and "world" are not strings even though they clearly fit the definition of what a string is, and facilities are provided to manipulate them? Pascal may be arguably better at manipulating text, but that's all a matter of opinion.

  11. #11
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    C has no native string awareness.
    Really?
    as whiteflags has pointed out."hello" is a string. (C string). Compiler does some work behind the scene.
    Btw, How come there's a memory leak when you are using local variable?
    Edit: not to mention that you can't assign array. c = s
    Last edited by Bayint Naung; 12-01-2010 at 01:24 AM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by CommonTater
    In C a string is simply an array of char that some library functions treat as a string.
    This is false. In C a string is not simply an array of char. The part that is missing is the fact that the contiguous sequence of characters has to be terminated by a null character in order to be considered a string.

    Frankly, I think that cas' point is pretty much correct: frankchester's string already is an array of char. It might not be terribly accurate if we want to be really pedantic (since string length and array size are two different things), but it does not matter since it gets the idea across to frankchester.

    Quote Originally Posted by CommonTater
    No, Pascal knows strings... lots of other languages know strings... they have data types for just the purpose and can manipulate them natively.
    Show me a programming language that can manipulate strings easily and I will show you a natural language that can talk

    In other words, there is no programming language that knows strings, because programming languages don't know, no more than natural languages talk or write. What you probably meant to say is that there is no primitive string data type in C, with strings being more of a standard convention with some support for string literals and with some library functions operating on the standard notion of a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    @laserlight, I am in total agreement with your point.
    Last edited by Bayint Naung; 12-01-2010 at 01:33 AM. Reason: ...

  14. #14
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I give up....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 08-16-2010, 10:00 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. converting string to integer, for further use
    By shoobsie in forum C Programming
    Replies: 2
    Last Post: 07-01-2005, 03:12 AM
  4. error converting string to char array
    By COBOL2C++ in forum C++ Programming
    Replies: 6
    Last Post: 07-11-2003, 10:59 AM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM