C代写/lab代写:lab 07 exercises

C代写/lab代写:lab 07 exercises

C代写/lab代写/c语言代写: 这是一个基础的c语言联系,着重考察c相关的基础知识

1. Files

Take the rot13.c program from the week 4 problem session (either your solution or my solution in /cmsfaculty/ajr/b09/ws/soln/04/rot13.c) and make it function like a normal “zero or more file names” unix tool. Also, make it take a (mandatory) rotation amount as argv[1] rather than always rotating by 13.

That is, argc is required to be at least 2, and argv[1] is the rotation amount; but after that, if argc is 2 it processes the standard input (as before), otherwise it loops through the remaining arguments, fopen()s each one, process it, fclose().

So instead of getchar() in your loop, you use getc(fp). You still use putchar().

Avoid duplicating code. When you go to process stdin, do so with the same code as you use for processing a file by filename, probably in a separate function, by making fp be the value stdin (defined in stdio.h).

2. Strings

2.1. Take the following code (available in /cmsfaculty/ajr/b09/lab/07/starter.c, or copy from this web page):

#include <stdio.h>

int main()
    char s[27];
    printf("%s\n", s);

and add a loop where indicated such that the program outputs “ABCDEFGHIJKLMNOPQRSTUVWXYZ” (without the quotes), without using any of the str* functions (e.g. strcpy). Just create the string ‘s’ by setting the items in the array to the successive letters of the alphabet using a simple for loop by doing arithmetic on character values, and then setting s[26] to zero.

2.2. Given an int value between 10 and 99 inclusive (e.g. start your code with “int x = 38;” for a test, or use the atoi() code we’ve been using on argv[1]), write code to convert this number to its string representation in a three-character array. The zeroth character of your string is the number div 10 + ‘0’; the next character of your string is the number mod 10 + ‘0’; and the array element two is the terminating zero. Note the difference between 0 and ‘0’ (the latter is 48 in ASCII).

3. To submit: string processing

A “palindrome” is a coherent phrase which consists of the same letters and digits forwards and backwards, although the spacing and capitalization may be different (in fact, all non-alphanumeric characters are irrelevant). Some examples:

	Madam, I'm Adam.
	A man, a plan, a canal, Panama.
	Doc, note: I dissent!  A fast never prevents a fatness.  I diet on cod.
	Won 12 tons?  Not 21 now.

Write a C program named “ispalindrome.c” which insists on having one argument (i.e. argc == 2). It checks its argument string to see whether it’s a palindrome, and exits with exit status 0 if it is a palindrome, or 1 if it is not. Similar to the test program, there is no output, unless there is a usage error.

An example of using ispalindrome is in the “findpal” program in /cmsfaculty/ajr/b09/lab/07/findpal , the execution of which looks like this:

$ /cmsfaculty/ajr/b09/lab/07/findpal even odd or never
never odd or even

(but only after you complete your “ispalindrome”! You may have to copy and modify the ‘findpal’ shell script to get it to run your ispalindrome in the right location.)

Implementation technique for ispalindrome.c: Start a pointer-to-char at the beginning of the string and another pointer-to-char at the end of the string. (Find the end of the string with a simple linear search to find the zero byte.) The pointer which started at the beginning of the string gets incremented and the pointer which started at the end of the string gets decremented, until they meet or pass.
So your loop will look something like “while (p < q)”.

Note: Since this exercise is about strings and pointers, you must use this pointer strategy to get the point for the lab. That is to say, your access to a character in the string has to look more like “*p” than like “s[i]”. (And “*(s+i)” doesn’t count as the pointer strategy, that’s the same as s[i] (by definition). You have to use a pointer variable which you are stepping through the string without indexing.)

Note relevant functions available after “#include <ctype.h>”: isalnum(c) returns whether the character c is alphanumeric (you should skip all non-alphanumeric characters in making your comparison), and tolower(c) returns a lower-case version of the character c if it’s an upper-case letter (e.g. tolower(‘E’) returns ‘e’), and returns the character itself otherwise (e.g. tolower(‘e’) returns ‘e’, and tolower(‘2’) returns ‘2’).

Submit your program with

        submit -c cscb09w18 -a lab07 ispalindrome.c


电子邮件地址不会被公开。 必填项已用*标注