Thread: Performing individual actions on chars in an array.

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

    Performing individual actions on chars in an array.

    I have an array of chars, I want to pluck each char out of the array, perform some actions on it and put it back into a new array in order.

    Can anyone explain how to do this? I figured I'd need a loop that is suitable for indefinite repetition, as I won't necessarily know how many chars are in the array, so just until either EOF or \0 is reached.

    Not sure what loop to use or how to do it?

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Well it depends. If you treat your array as a string(meaning that you always have \0 at the end) then you can just initialize your character to the start of the array, loop until you reach \0, increase the index where you are taking the character from. This can easily be done in a for loop.

    Then depending on what you decide to do with that character you can add it to your new array.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    The easiest way would probably be to create a second array that you can put the newly ordered chars into. The distinction between character array and string is important, and I'm not quite clear by your post which one you're working. The difference is, a string will definitely have a terminating '\0' character, but a character array might not.

    Assuming you're dealing with a string, you might do something like this:
    Code:
    char *dostuff(char *original_string)
    {
      char *new_string = malloc(strlen(str) + 1);
      int i;
    
      for(i = 0;original_string[i] != '\0';++i)
      {
        char ch = original_string[i];
        // "Perform some actions" here
        new_string[some_index] = ch;
      }
      return new_string;
    }
    Make sure you free() your memory when you're done with it and be sure the new string is terminated with a '\0'.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    13
    Quote Originally Posted by itsme86 View Post
    The easiest way would probably be to create a second array that you can put the newly ordered chars into. The distinction between character array and string is important, and I'm not quite clear by your post which one you're working. The difference is, a string will definitely have a terminating '\0' character, but a character array might not.

    Assuming you're dealing with a string, you might do something like this:
    Code:
    char *dostuff(char *original_string)
    {
      char *new_string = malloc(strlen(str) + 1);
      int i;
    
      for(i = 0;original_string[i] != '\0';++i)
      {
        char ch = original_string[i];
        // "Perform some actions" here
        new_string[some_index] = ch;
      }
      return new_string;
    }
    Make sure you free() your memory when you're done with it and be sure the new string is terminated with a '\0'.
    Thanks, yeah I'm using a string I think. Now I just have to work out what on earth that piece of code means. I'm only a beginner to the entire of programming

    Could you explain what the 'char *new_string = malloc(strlen(str) + 1);' section does? I get the 'for' loop as it pulls each char in order (I'll be converting them to their ASCII values). Then I guess the new_string[index]=ch will add the character back into the new array? I'm confused about exactly how that bit works.
    Last edited by frankchester; 12-04-2010 at 10:36 AM.

  5. #5
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by frankchester View Post
    Thanks, yeah I'm using a string I think. Now I just have to work out what on earth that piece of code means. I'm only a beginner to the entire of programming

    Could you explain what the 'char *new_string = malloc(strlen(str) + 1);' section does? I get the 'for' loop as it pulls each char in order (I'll be converting them to their ASCII values). Then I guess the new_string[index]=ch will add the character back into the new array? I'm confused about exactly how that bit works.
    The malloc allocates memory dynamically for a string of length str + 1(to store the \0) and then returns a pointer to that data. So now, you have new_string pointing to that memory location where you will have your string. free(new_string) will do the exact opposite, meaning it will free the memory allocated once you are done using it.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading chars from file into array
    By AJOHNZ in forum C++ Programming
    Replies: 1
    Last Post: 08-19-2009, 03:37 PM
  2. Read 1000000 chars into array.
    By nenpa8lo in forum C Programming
    Replies: 10
    Last Post: 05-06-2008, 06:08 AM
  3. Getting a 2 byte number from an array of char's
    By kzar in forum C Programming
    Replies: 20
    Last Post: 04-10-2005, 10:18 PM
  4. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM
  5. passing char where array of chars required
    By larry in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2001, 01:24 PM