Thread: Comparing string arrays

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    Comparing string arrays

    I'm trying to get my program to store only letters in two arrays. So far I have:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    main () {
    
       printf("Enter something: ");
       char temp[100];
    
       fgets(temp, 100, stdin);
    
       int c;
       int letters = 0;
       for (c = 0; c <= strlen(temp); c++) {
          if (isalpha(temp[c])) {
             letters++;
               }
             }
    
       int *ptr;
       ptr = &letters;
       char str[*ptr];
       char rev[*ptr];
       printf("Letters amount: %d\n", *ptr);
    
       int i;
       int k = 0;
       for (i = 0; i < strlen(temp); i++) {
       if (isalpha(temp[i])) {
             str[k] = temp[i];
             k++;
               }
            }
    
       k = 0;
       for(i = strlen(str)-1; i >= 0; i--){
    
            rev[k] = str[i];
          k++;
        }
    
    
    
       printf("You entered: %s\n", str);
       printf("The reverse: %s\n", rev);
    
    }
    If I put "Test":
    You entered: Test
    The reverse: tseT

    If I put "Testing One Two Three":
    You entered: TestingOneTwoThree
    The reverse: eerhTowTenOgnitseT@

    If I put "Testinggg":
    You entered: Testingggggaj<@
    The reverse: @<jagggggnitseT

    I have to get str[] and rev[] to store only letters because I will have to compare them later.

    I understand that it has something to do with the null-byte terminator in arrays, so how can I adjust my code so that it stores the letters correctly?
    Last edited by never_lose; 03-20-2011 at 12:47 AM. Reason: To make my code neater.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would use getc() and if isalpha() then put that char into array[lettercount++], otherwise, don't increment lettercount.

    Generally, it's easier to test data before you put it into an array, then to fix it up, after it's already in it, and now you have to delete parts of it, shuffle other parts around to close the gaps, etc.

    Make sense?

    If your isalpha(temp[c]), was assigned to Array[letters++], in the first loop, I think you'd be almost there wouldn't you?

    And welcome to the forum, Never_lose!

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Not sure how I would use getc() here. Any good examples? I tried using it in place of fgets(), but it doesn't scan user input. Do I use it after fgets()?

    In array[lettercount++], what does the ++ do? And should I store my letters in an array like this?

    And welcome to the forum, Never_lose!
    Thanks!

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'll post an example in an edit to this post.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define SIZE 100
    
    int main() {
      int i, n;
      char s[SIZE]="";
      char ch;
      printf("\n\nEnter some letters and numbers, and etc. Only letters will go into the array s[]\n");
      i=0;
      while((ch=getc(stdin)) != EOF) { //EOF is Ctrl+Z
        if(isalpha(ch)) {
          s[i++]=ch;
        }
      }
    
      printf("\n%s", s);
    
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }
    variable++ increments the variable by one.

    Yes, you should use a char array.
    Last edited by Adak; 03-20-2011 at 12:15 AM.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    In your code, how do I enter after I type something? Pressing enter only goes to a new line.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    I'll post an example in an edit to this post.
    Code:
      char ch;
      printf("\n\nEnter some letters and numbers, and etc. Only letters will go into the array s[]\n");
      i=0;
      while((ch=getc(stdin)) != EOF) { //EOF is Ctrl+Z
    The value EOF cannot be stored in a char. It is a negative integer type, that holds no char representable value.


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

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Hit Ctrl+z, then the Enter key to quit.

    @Quzah:

    Why can't an unsigned char be negative? My unsigned chars (the default), have a range from -128 to 127.
    Last edited by Adak; 03-20-2011 at 12:36 AM.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Tried Ctrl+Z. After that, enter does nothing, I can't type anything, and I can't move the insertion point anywhere.

    I'm using Netbeans. When I compile, it opens in command prompt.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Are you on a Linux system? If so use Ctrl+d, instead of Ctrl+z.

    If you're on Windows, you should see a ^Z as one char, on screen, when you hit Ctrl+z.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    @Quzah:

    Why can't an unsigned char be negative?
    What? Why can't unsigned be negative? Did you really just type that?
    Quote Originally Posted by Adak View Post
    My unsigned chars (the default), have a range from -128 to 127.
    It doesn't matter. EOF is guaranteed to not be a negative value that is representable in a char. It's not guaranteed to be -1. There's a reason all those scores of standard functions return ints instead of chars.


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

  11. #11
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    I'm on Windows. Nothing appears after I hit ctrl+z.

    But when I run this on an Unix machine from my Windows computer it works.

    Any idea why it won't work in Netbeans?

  12. #12
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    OK, thanks Quzah.

    So Never_lose, if you can't get it to work, your EOF is unrepresentable as a char. I'll post up a new version, with int's instead.

    The netbeans system has a different EOF value. Most systems used -1, years ago, but now the standards committee for C decided to change EOF to be a non-char value.

    It works fine for me, but let's make it something standard.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    #define SIZE 100
    
    int main() {
      int i, n;
      char s[SIZE]="";
      
      printf("\n\nEnter some letters and numbers. Only letters will go into s[]\n");
      i=0;
      while ((n = getchar()) != '\n') {
        if(isalpha(n)) 
          s[i++] = n;
      }
    
      printf("\n%s", s);
    
      printf("\n\n\t\t\t     press enter when ready");
      (void) getchar(); 
      return 0;
    }
    Last edited by Adak; 03-20-2011 at 01:16 AM.

  13. #13
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Ok, thanks... and what is the purpose of (void) getchar();?

    Never mind, it must be so that the user can "press enter when ready."

    I've finally finished my program by the way, thanks for the help!
    Last edited by never_lose; 03-20-2011 at 01:57 AM.

  14. #14
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Adak View Post
    Most systems used -1, years ago, but now the standards committee for C decided to change EOF to be a non-char value.
    No, it's always been that way. They didn't just change it.

    The problem is that if you are say reading a binary file one byte at a time, and you are taking those bytes and sticking them in a char, and you are testing that against EOF, then some random byte in your file will trigger EOF in your comparison. You'll think you've hit the end of your file when you haven't.


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

  15. #15
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Question 12.1b
    if you have read K&R since that time eof is negative value.
    Because it works with my turbo c for tenty years does not make it correct.
    Last edited by Bayint Naung; 03-20-2011 at 01:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. String Compare Using Dynamic Arrays
    By djwicks in forum C Programming
    Replies: 4
    Last Post: 03-31-2005, 08:01 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM
  4. string arrays
    By Raison in forum C Programming
    Replies: 27
    Last Post: 10-02-2003, 06:27 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM