Sponsored Content
Top Forums Programming Programming Challenges - A List Post 302119169 by Octal on Monday 28th of May 2007 10:47:10 AM
Old 05-28-2007
Sucessfully does english to morse, but fails with morse to english (probaly the best I'll get):
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

const char e[] = {
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
	'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
	'Y', 'Z',
	'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
	' '
};
const char *m[] = {
	".- ", "-.. ", "-.- ", "-.. ", ". ", "..-. ", "--. ", "....",
	"..", ".--- ", "-.- ", ".-.. ", "-- ", "-. ", "--- ", ".--. ",
	"--.- ", ".-. ", "... ", "- ", "..- ", "...- ", "...- ",
	".-- ", "-..- ", "-.-- ", "--.. ",
	".---- ", "..--- ", "...-- ", "....- ", "..... ", "-.... ",
	"--... ", "---.. ", "----. ", "----- ",
	"   "
};

void morse(char []);
void unmorse(char []);
void help(char []);

main(int argc, char *argv[]) {
	if (argc < 3 || strstr(argv[1], "-h") != NULL) {
		help(argv[0]);
		exit(1);
	}
	if (strstr(argv[1], "-e") != NULL) {
		morse(argv[2]);
	} else if (strstr(argv[1], "-m") != NULL) {
		unmorse(argv[2]);
	} else {
		fprintf(stderr, "unkown option: %s\n\n", argv[1]);
		help(argv[0]);
		exit(1);
	}
	
}
void morse(char s[]) {
	int i = 0, j;

	while (s[i] != '\0') {
		j = 0;
		while (s[i] != e[j] && s[i] != (e[j] + 'a' - 'A')) {
			j++;
		}
		printf("%s", m[j]);
		i++;
	}
	printf("\n");
}
void unmorse(char s[]) {
	int i = 0, j;

	while (s[i] != '\0') {
		j = 0;
		while (strstr(&s[i], m[j]) != NULL &&
		       strstr(m[j], &s[i]) != NULL) {
			j++;
		}
		printf("%s", e[j]);
		i++;
	}
}
void help(char prog[]) {
	fprintf(stderr, "usage: %s [options] [string]\n"
		"options:\n\t-e\tenglish to morse\n"
		"\t-m\tmorse to english\n\t-h\tprint this\n",
		prog);
}

I got another challenge (yes, it's conversion):

Challenge: Sucessfully implement rot13: english to rot13.

Difficulty: (really) Easy

Helpful Information: http://en.wikipedia.org/wiki/ROT13

Rot was really easy (though I wasn't anal about error checking):
Code:
#include <stdio.h>

char rot(char [], int, int);
char lower(char [], int);

main(int argc, char *argv[]) {
	int i = 0;

	if (argc == 1) {
		fprintf(stderr, "usage: %s [rot] [string]\n", argv[0]);
	}
	while (argv[2][i] != '\0') {
		argv[2][i] = lower(argv[2], i);
		argv[2][i] = rot(argv[2], i, atoi(argv[1]));
		i++;
	}
	printf("%s\n", argv[2]);
}
char lower(char s[], int i) {
	if (s[i] <= 90 && s[i] >= 65)
		s[i] += 32;
	return s[i];
}
char rot(char s[], int i, int rotnum) {
	return (s[i] > 109) ? (s[i] -= rotnum) : (s[i] += rotnum);
}


Last edited by Octal; 05-28-2007 at 12:06 PM..
 

3 More Discussions You Might Find Interesting

1. AIX

AIX 6.1 IDSLDAP Installation Challenges

Please bare with me, since I am new to AIX and LDAP. I am attempting to install idsldap server on our AIX 6.1 NIM server. I installed the following packages: root@nim(/)# lslpp -l|grep ldap db2_08_01.ldap 8.1.1.80 COMMITTED DB2 LDAP Support idsldap.clt64bit61.rte 6.1.0.17 COMMITTED... (6 Replies)
Discussion started by: ecollins
6 Replies

2. UNIX for Advanced & Expert Users

Challenges in finding and copying the block

Hi, I have a below challenging task where iam unable to find the block and copy the same into a file. I tried my luck,howver iam unable to reach the first and second step..Can anyone help me with a clue or with the commands so that i can give a try. 1. search the <number>9966993366</number>... (2 Replies)
Discussion started by: cskumar
2 Replies

3. Shell Programming and Scripting

Korn shell script - SQL statement challenges

Hi scripting experts. I have some coding challenges that I'm hoping you can help me out. I have one file#1 that contains the following sql statement that spans over multiple lines: sql Select /*+ use_has(a,b) */ * from customer a, customer_address b where a.id = b.id... (1 Reply)
Discussion started by: pchang
1 Replies
GETOPT(3)						     Library Functions Manual							 GETOPT(3)

NAME
getopt - get option letter from argv SYNOPSIS
int getopt(argc, argv, optstring) int argc; char **argv; char *optstring; extern char *optarg; extern int optind; DESCRIPTION
Getopt returns the next option letter in argv that matches a letter in optstring. Optstring is a string of recognized option letters; if a letter is followed by a colon, the option is expected to have an argument that may or may not be separated from it by white space. Optarg is set to point to the start of the option argument on return from getopt. Getopt places in optind the argv index of the next argument to be processed. Because optind is external, it is normally initialized to zero automatically before the first call to getopt. When all options have been processed (i.e., up to the first non-option argument), getopt returns EOF. The special option -- may be used to delimit the end of the options; EOF will be returned, and -- will be skipped. DIAGNOSTICS
Getopt prints an error message on stderr and returns a question mark (?) when it encounters an option letter not included in optstring. EXAMPLE
The following code fragment shows how one might process the arguments for a command that can take the mutually exclusive options a and b, and the options f and o, both of which require arguments: main(argc, argv) int argc; char **argv; { int c; extern int optind; extern char *optarg; . . . while ((c = getopt(argc, argv, "abf:o:")) != EOF) switch (c) { case `a': if (bflg) errflg++; else aflg++; break; case `b': if (aflg) errflg++; else bproc(); break; case `f': ifile = optarg; break; case `o': ofile = optarg; break; case `?': default: errflg++; break; } if (errflg) { fprintf(stderr, "Usage: ..."); exit(2); } for (; optind < argc; optind++) { . . . } . . . } HISTORY
Written by Henry Spencer, working from a Bell Labs manual page. Modified by Keith Bostic to behave more like the System V version. BUGS
It is not obvious how `-' standing alone should be treated; this version treats it as a non-option argument, which is not always right. Option arguments are allowed to begin with `-'; this is reasonable but reduces the amount of error checking possible. Getopt is quite flexible but the obvious price must be paid: there is much it could do that it doesn't, like checking mutually exclusive options, checking type of option arguments, etc. 4.3 Berkeley Distribution May 27, 1986 GETOPT(3)
All times are GMT -4. The time now is 05:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy