Some structure and Syntax Help Please!

This is a discussion on Some structure and Syntax Help Please! within the C Programming forums, part of the General Programming Boards category; This is a palindrome assignment. The program needs to be divided into three files. 1 header, 2 .c files. The ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    7

    Some structure and Syntax Help Please!

    This is a palindrome assignment. The program needs to be divided into three files. 1 header, 2 .c files.

    The main .c file is pal.c and it's purpose is only to read a file from the command line. using either ./pal < file.txt ...or ./pal files.txt
    then it calls the function that is in checkpal.c

    In checkpal.c ...the requirement is to use arrays and not pointers to store and manipulate the palindrome. In this file is also the printing of whether it is a palindrome or not. In addition to this, it is obvious that we have to exclude whitespace, as well as punctuation. Also, I must convert uppercase characters to lower case.

    With that said. I've done this code. In three different files. But i'm unsure of my structure and syntax. Please bare in mind that i'm really new to this stuff. I have great difficulty in figuring out how to use all these functions and statments and arguments.

    If it would be possible to explain the problems and errors in really simple terms, as this stuff doesn't come easy to me, it would be really apprieciated.

    pal.c
    Code:
    #include <stdio.h>
    #include "checkpal.h"
    
    void read(FILE *fp);
    
    int main(int argc, char *argv[])
    {
        int  count; 
    
        FILE  *fin; 
    	
        if (argc == 1)
            Checkifpal();
        else {
            for (count = 1; count < argc; count++) {
                if (argc > 2)
                    printf("\n==> %s <==\n", argv[count]);
                if ((fin = fopen(argv[count], "r")) == NULL)
                    fprintf(stderr, "Cannot Open File %s\n", argv[count]);
                else {
                   read(fin);
                }
            }
        }
        return 0;
    }
    
    void read(FILE *fp)
    {
        int  lines;
        char  buf[BUFSIZ];
    
        for (lines=0; lines < MAX_LINES; lines++) {
            if (fgets(buf, BUFSIZ, fp) == NULL)
                return;
            printf("%s", buf);
        }
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    and this is the checkpal.c file:

    which as you can probably see...i'm very unsure about alot of it.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    #include "checkpal.h"
    
    int Checkifpal(void){
    
    int r, l;
    int strlen1(char buf[]);
    int len = 0;
    char fwd[len];
    	
           for (r=0; r < len; r++){         /* 1st for loop reads from right and increments*/
    	        if ( MAX_LINES == r)
    	        return r;
           return (-1);
           }
    
    
    int strlen2(char fwd[]);
    int len2 = 0;
    char bkward[len2];
    char buf[MAX_LINES];
    
           for (l=len2; l > 0; l--)	{       /* 2nd for loop reads from left and decrements*/
    	        if (MAX_LINES == l)
    	        return l;
           return (-1);
           }
    
    /*compare from two for loops and print*/
    
    	if (fwd == bkward) {               
    	    printf("yes/t%s", buf);      /*print yes, tab space and then prints the buf string*/
            }	
    	else {
    		printf("no/t%s", buf);      /*otherwise if != print no, tabspace, buf string*/
            }
    	return 0;
    }
    
    /* Removes non alphanumeric */
    
    int main(void)
    {
    
     int pos;
     int dest = 0;
     char buf[BUFSIZE];
    
    
    	for (pos = 0; buf[pos] != '\0'; pos++)
    	{
    		if (isalnum(buf[pos]))
    			buf[dest++] = buf[pos];
    	}
    	buf[dest] = '\0';
    
    /* Convert upper to lower case */
    
    	for (pos=0;buf[pos] != '\0'; pos++)
    	{
    		if (isupper(buf[pos])!= 0)
    			buf[pos] = tolower(pos);
    		else
    		{
    			buf[pos] = pos;
    		}
    	}
     return();
    }
    
    /*Convert upper to lower case #2 using while instead of for
    
     int i =0;
     char array[BUFSIZE];
    
     while (buf != EOF)
    {
    	if (buf >= 'A' && buf <= 'Z')
    		buf = tolower(buf);
    	array[i++] = buf;
    }
     array[i] = '\0';
     puts (array);
     return 0;
    }
    */
    and the header file:

    Code:
    #ifndef _CHECKPAL_H_
    #define _CHECKPAL_H_
    
    #define BUFSIZE     40
    #define MAX_LINES  500  
    
    int Checkifpal(void);
    
    #endif

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Well, let me start with pal.c

    The way I'd go about this is:
    Code:
    if they gave a filename
      open filename
      set readfile to file stream you just opened
    else
      set readfile to stdin
    call Checkifpal(readfile)
    if readfile does not equal stdin
      close readfile
    Then modify your Checkifpal() function to accept a FILE *. Checkifpal() would then read in each line as it comes in with fgets()...it doesn't need to know if it's coming from stdin or a file because a stream is a stream...main() handles all the dirty work for it.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    7
    okay. i think i get that.

    what about the checkpal.c

    i know that probably is a nightmare to programmers...lol

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,175
    Well, Checkispal() would do something like this:
    Code:
    while fgets()
    {
      set first var equal to 0
      set second var equal to length of string minus 1
      while first var is less than or equal to second var
      {
        skip whitespace at first var (increment)
        skip whitespace at second var (decrement)
        capitalize characters in string at index of first and second vars
        if those characters are not equal
        {
           string is not a plandrome
        }
        increment first var
        decrement second var
      }
    }
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 10:15 PM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 01:17 AM
  3. Includes making me insane >.<
    By IceDane in forum C Programming
    Replies: 14
    Last Post: 04-14-2008, 11:24 AM
  4. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21