COMP1721 - Higher Computing 1B

Higher Computing 1B - Week Laboratory Exercises


#include <stdio.h>

/*
 * Read a String and then print the letters one per line
 *
 * written by Andrew Taylor andrewt@cse.unsw.edu.au
 * August 2001 as a COMP1721 lab exercise
 */
 
enum {
	MAXLINE = 80
};

int
main(int argc, char *args[]) {
    char line[MAXLINE];
    int  i;
    
    printf("Enter a string: ");
    fgets(line, MAXLINE, stdin);
    
    for (i = 0; line[i] != '\0' && line[i] != '\n' ; i++)
    	printf("%c\n", line[i]);

    return 0;
}


#include <stdio.h>

/*
 * Read a String and then indicate if it is a palindrome
 *
 * written by Andrew Taylor andrewt@cse.unsw.edu.au
 * August 2001 as a COMP1721 lab exercise
 */
 
enum {
	MAXLINE = 80,
};

int
main(int argc, char *args[]) {
    char line[MAXLINE];
    int  left, right;
    
    printf("Enter a string: ");
    fgets(line, MAXLINE, stdin);
    
    left = 0;

    for (right = 0; line[right] != '\0' && line[right] != '\n' ; right++)
    	;

    if (right == 0)
    	return 0;
 
	right--;

	while (left < right) {
		if (line[left] != line[right]) {
		    printf("String is not a palindrome\n");
		    return 0;
		}
		left++;
		right--;
	}

	printf("String is a palindrome\n");
    return 0;
}


#include <stdio.h>
#include <ctype.h>

/*
 * Read a String and then indicate if it is a palindrome
 *
 * written by Andrew Taylor andrewt@cse.unsw.edu.au
 * August 2001 as a COMP1721 lab exercise
 */
 
enum {
	MAXLINE = 80
};

int
main(int argc, char *args[]) {
    char line[MAXLINE];
    int  left, right ;
    
    printf("Enter a string: ");
    fgets(line, MAXLINE, stdin);
    
    left = 0;

    for (right = 0; line[right] != '\0' && line[right] != '\n' ; right++)
    	line[right] = tolower(line[right]);

    if (right == 0)
    	return 0;
 
	right--;

	while (left < right) {
		if (!islower(line[left])) {
			left++;
			continue;
		}
		if (!islower(line[right])) {
			right--;
			continue;
		}
		if (line[left] != line[right]) {
		    printf("String is not a palindrome\n");
		    return 0;
		}
		left++;
		right--;
	}

	printf("String is a palindrome\n");
    return 0;
}


#include <stdio.h>
#include <ctype.h>

/*
 * Counts the frequency of
 * letters read from standard input. 
 *
 * written by Andrew Taylor andrewt@cse.unsw.edu.au
 * August 2001 as a COMP1721 lab exercise
 */
 
enum {
	MAXLINE = 80
};

int
main(int argc, char *args[]) {
    char line[MAXLINE];
    int  alphabeticCount = 0;	   /* how many non-alphabetic letters read  */
    int  nonAlphabeticCount = 0;   /* how many non-alphabetic letters read  */
    int  lineCount = 0;			   /* how many lines                        */
    int  letterCount[26];	       /* frequency of alphabetic letters       */
    int  i;
    
 	for (i = 0; i < 26; i++)
 	    letterCount[i] = 0;
 	    
    for (lineCount = 0;;lineCount++) {
    	if (fgets(line, MAXLINE, stdin) == NULL)
    		break;
    
		for (i = 0; line[i] != '\0'; i++) {
			if (islower(line[i])) {
				alphabeticCount++;
				letterCount[line[i] - 'a']++;
			} else if (isupper(line[i])) {
				alphabeticCount++;
				letterCount[line[i] - 'A']++;
			} else
				nonAlphabeticCount++;
		}
	}
	
	printf("%d lines of input read.\n", lineCount);
	printf("%d alphabetic characters read.\n", alphabeticCount);
	printf("%d non-alphabetic characters read.\n", nonAlphabeticCount);
	printf("The frequencies of the alphabetic characters was:");
	
	for (i = 0; i < 26; i++) {
		if (letterCount[i] == 0)
			continue;
		printf("'%c' %d\n", 'a' + i, letterCount[i]);
	}
    return 0;
}


Andrew Taylor (andrewt@cse.unsw.edu.au)
Higher Computing 1B, Computer Science & Engineering, UNSW