Thread: Getting rid of array index brackets

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    11

    Question Getting rid of array index brackets

    Just seeking advice. I have a program for homework that I completed and runs great. The program encrypts letters. A = Z, B = Y, C = X etc. and adds 3 to ascii value for digits. The only problem is that Array index brackets should NOT APPEAR ANYWHERE in my code EXCEPT when i referr to the argv contents.

    I am just seeking advice on how to go about removing the array index brackets, only line[] needs to be taken out. here is my code.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
     
    
    #include<ctype.h>
    
    #define MAX_SIZE 256
    
    char encryptAlpha  (char strChar);
    char encryptNumber (char strNum);
    
    int main (int argc, char *argv[])
    { 
    	int i;
    	char **mover;
    	FILE *fpIn;
    	FILE *fpOut;
    	char line[MAX_SIZE];
    	int size;
    	
    
    	for (i = 0; i < argc; i++)
    	{
    		printf("Argument %d is %s\n", i, argv[i]);
    	}
    	printf ("\n");
    	for (mover = argv; *mover != NULL; mover++)
    	{
    		printf("Argument pointed at by mover is %s\n", *mover);
    	}
    
    	if(!(fpIn = fopen(argv[1], "r" )) )
    	{
    		printf( "Error opening the input file.\n" );
    		exit(101);
    	}
    
    	if(!(fpOut = fopen(argv[2], "w" )) )
    	{
    		printf( "Error creating output file.\n" );
    		exit(101);
    	}
    
    	while(fgets(line, MAX_SIZE, fpIn))
    	{
    		size = strlen(line);
    		for (i = 0; i < size; i++)
    		{
    			if isalpha(line[i])
    				line[i] = encryptAlpha(line[i]);
    			else if ((line[i] >= '0') && (line[i] <= '9')) 
    				line[i] = encryptNumber(line[i]);
    		}
    	fprintf (fpOut, "%s", line);
    	}
    }
    
    char encryptAlpha (char strChar)
    {
    
    //	Local Definitions
    	char firstChar;
    	char lastChar;
    
    //	Statements
    	if ((strChar >= 'A') && (strChar <= 'Z'))
    	{
    		firstChar = 'A';
    		lastChar  = 'Z';
    		{
    			while ((firstChar <= 'M') && (lastChar >= 'N'))
    			{
    			if (strChar == firstChar)
    				strChar = lastChar;
    			else if (strChar == lastChar)
    				strChar = firstChar;
    
    			firstChar++;
    			lastChar--;
    			}
    		}
    	}
    
    	if ((strChar >= 'a') && (strChar <= 'z'))
    	{
    		firstChar = 'a';
    		lastChar  = 'z';
    		{
    			while ((firstChar <= 'm') && (lastChar >= 'n'))
    			{
    			if (strChar == firstChar)
    				strChar = lastChar;
    			else if (strChar == lastChar)
    				strChar = firstChar;
    
    			firstChar++;
    			lastChar--;
    			}
    		}
    	}
    return strChar;
    }// encryptAlpha
    
    char encryptNumber (char strNum)
    {
    	strNum += 3;
    
    return strNum;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    arr[ i ] == *(arr + i)
    as any book should tell you.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    #include<stdio.h>
    int main( void )
    {
        char array??(??) = "hello world";
        int x;
        
        for( x=0; array??(x??); x++ )
            putchar( array??(x??) );
            
        return 0;
    }
    It's always fun to see just what you can get away with by bending the wording of the question.


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

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Ha Ha - nice one Quzah
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. [C] Sorting an array and preserving the original index
    By frodo_jedi in forum C Programming
    Replies: 10
    Last Post: 04-06-2009, 06:51 AM
  3. Reversing character array without accessing thro' index.
    By Roaring_Tiger in forum C Programming
    Replies: 8
    Last Post: 08-28-2004, 10:52 AM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Array Program
    By emmx in forum C Programming
    Replies: 3
    Last Post: 08-31-2003, 12:44 AM