Thread: Getting a character from a char one at a time

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    21

    Getting a character from a char one at a time

    Is there any way I can extract a character from a char one at a time? i.e. if its 100 characters in total what I'd like to be able to do is put a for loop which takes every character one at a time from within the char, does a comparison on it, and if it evaluates to false it appends the current character to a new char, if it evaluates to true it appends a different character to it. Basically what I am trying to achieve is searching through a long char and replacing certain characters for other characters.

    Many thanks to anyone who can help.

    Godders_2k

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You mean a char array - because "a char" is one character.

    Yes, you can. Just use a loop to iterate over the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    So could you explain the following to me:

    I've declared a variable as char *szTemp

    Inside there I have stored a big long bunch of characters, i.e. DwPSDsfAercecasdaaaofidsjafaplasdfafaasdfa

    If a char is just one character how can it store all of these characters?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You need a char array.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    But surely even if I had a char array, it would just store all of the long list of characters into one of the array locations? It wouldn't store one letter per array location?

    e.g.

    Array Location
    1 dweroaasdfopasjdfapsodjfapdsffcsdsc
    2 NULL
    3 NULL
    4 NULL
    5 NULL
    6 NULL

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    A char array will contain one char per location - otherwise you have something other than a char array - what you describe looks like an array of char pointers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    Oh right! So sorry to be a complete novice but how do I declare a char array?

  8. #8
    Registered User Welder's Avatar
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    100
    Godders_2k, a char is one character, but what you have is a pointer.

    You can allocate some memory and point szTemp to it then fill it up with your data.

    Example off the top of my head:

    Code:
    char *szTemp;
    int i=0;
    
    szTemp=(char*)malloc(length of your string goes here);  // Allocate some memory to store your string
    strcpy(szTemp,"DwPSDsfAercecasdaaaofidsjafaplasdfafaasdfa");  // copy your string into memory
    
    while(szTemp[i] != '\0')   //  Loop until until the end of string is found (NULL character is string terminator)
    {
        //Do something here.  Current char can be gotten with:    szTemp[i];
        switch(szTemp[i++])  // This is where we find out what the character is.  i++ advances location in memory to find the next character
        {
            case 'A':
            {
                  printf("We found A at position %d in the string",i);
                  printf("Another way of printing our current character which is %c",szTemp[i]);
            }
            break;
            case 'i':
            {
                  printf("We found I at position %d in the string",i);
                  printf("Another way of printing our current character which is %c",szTemp[i]);
            }
            break;
        }
    }
    
    free(szTemp);  // Make sure to free up the memory you allocated when you are finished
    That is just one way of doing it.
    Last edited by Welder; 11-07-2007 at 11:16 AM.

  9. #9
    Registered User Welder's Avatar
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    100
    Quote Originally Posted by Godders_2k View Post
    But surely even if I had a char array, it would just store all of the long list of characters into one of the array locations? It wouldn't store one letter per array location?

    If you want to store one character per location in the array you would define your variable as:

    char blah[numberofitemsinarray];

    If you want an array of strings you would define it as:

    char *blah[numberofitemsinarray];

    If using the latter method you need to allocate memory to each item in the array as I showed in my example but a little different.

    blah[number]=(char*)malloc(sizeofstringgoeshere);

    Then free each one when you are done with them with:

    free(blah[number]);
    Last edited by Welder; 11-07-2007 at 11:18 AM.

  10. #10
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    Hi,

    Thanks for the response its really helpful. Is it possible to declare variables midway through a script or do you have to do them all at the beginning for C? Basically I was going to declare a new char array once I knew what the size of it had to be (i capture a string and then measure the length of it). However, it doesn't appear to like doing this and so I was just wondering whether all the variables had to be declared at the start.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Welder, I know you mean well, but in general it's not a good idea to post partial or full solutions to what is most likely school homework/assignments. It doesn't help the student one least bit in the end, because what you learn from copying others is just "that it's easy to copy others".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  12. #12
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    I wish I was still in school! I also wish I could get his solution working but I can't.

  13. #13
    Registered User Welder's Avatar
    Join Date
    Oct 2007
    Location
    Washington
    Posts
    100
    Quote Originally Posted by matsp View Post
    Welder, I know you mean well, but in general it's not a good idea to post partial or full solutions to what is most likely school homework/assignments. It doesn't help the student one least bit in the end, because what you learn from copying others is just "that it's easy to copy others".
    I didn't read anything about this being a homework assignment. If everybody went with what you are saying, nobody would get help on this forum at all.

    I would be pretty ticked if someone came into a thread of mine and told people not to help me because it may be giving me answers for school work.

    If it is homework, and he is cheating his way through school it will show later on and either he will fail his class, not get a job, or if he does get a job get fired because he doesn't know his stuff. If anything, giving answers to someone in school is weeding out the cheating types from the workplace in the future.

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    What about giving answers that are way more complicated than they have to be? That doesn't really help the original poster learn anything.

    It's really quite simple.
    Code:
    char data[] = "DwPSDsfAercecasdaaaofidsjafaplasdfafaasdfa";
    int x, len = strlen(data);
    
    for(x = 0; x < len; x ++) {
        /* do something with data[x] */
    }
    Note that I declared data as an array (with []) rather than as a pointer (with *). With an array, you can modify the contents; with a pointer, you cannot. However, a pointer might be more memory-efficient, so if you don't need to modify the data, a pointer is a good idea.

    As an example, inside the for loop, you could replace every space with a newline.
    Code:
    if(data[x] == ' ') data[x] = '\n';
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Nov 2007
    Posts
    21
    Thanks Dwk and welder your help is really appreciated. And in response to this being a homework assignment. I am a software tester who has recently started doing some performance testing. I have pretty much next to no programming knowledge apart from a little bit of java. I appreciate what is being said about homework assignments and agree with it to an extent but not giving help on the off chance thats what it is doesn't really help the people who are just wanting to learn. Believe me I wouldn't be bothering you experts if I hadn't spent best part of 2-3 days looking around on the internet and trying to learn more about C. You would also be pleased to know I have ordered a book and hopefully will be an expert myself but in the meantime all the help everyone gives me is really great and i'm very appreciative of the time you take to write responses to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 09:40 PM
  5. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM