Thread: Comparing string arrays

Threaded View

Previous Post Previous Post   Next Post Next Post
  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.

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