Programming Challenges - A List


 
Thread Tools Search this Thread
Top Forums Programming Programming Challenges - A List
# 8  
Old 05-28-2007
Yes, I haven't. But I had a special condition for handling the ' ', but I forgot the 'continue' after that so it was falling through the rest of the while loop. Anyway, taken care of. Thanks
# 9  
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..
# 10  
Old 06-01-2007
Here is my shot!

Not thoroughly tested! Smilie

Code:
#include <stdio.h>
#include <string.h>

#define CONS 13
#define UPPER 65
#define LOWER 97

void rotate(char, int);

int main() {
  char *str = "zypher";
  int length;
  int i;

  for( length = strlen(str), i=0; i<length; i++ ) {
    if( str[i] >= UPPER && str[i] <= (UPPER + CONS*2 - 1) ) {
      rotate(str[i], UPPER);
    }
    else if( str[i] >= LOWER && str[i] <= (LOWER + CONS*2 - 1) ) {
      rotate(str[i], LOWER);
    }
  }
  return 0;
}

void rotate(char ch, int start) {
  (ch + CONS) > (start + CONS*2 - 1) ?  printf("%c\n", ( ch + CONS ) - ( (start + CONS*2 - 1) + 1 ) + start) : printf("%c\n", ch + CONS);
}

# 11  
Old 06-01-2007
is awk counted ? Smilie
Code:
#!/usr/bin/awk
# Function: convert between morse and english
# Limitations: Many !!
BEGIN{
          morse["A"]=".-"   ; morse["B"]="-..."
          morse["C"]="-.-." ; morse["D"]="-.."
          morse["E"]="."    ; morse["F"]="..-."
          morse["G"]="--."  ; morse["H"]="...." 
          morse["I"]=".."   ; morse["J"]=".---"
          morse["K"]="-.-"  ; morse["L"]=".-.." 
          morse["M"]="--"   ; morse["N"]="-."
          morse["O"]="---"  ; morse["P"]=".--."
          morse["Q"]="--.-" ; morse["R"]=".-." 
          morse["S"]="..."  ; morse["T"]="-"
          morse["U"]="..-"  ; morse["V"]="...-"
          morse["W"]=".--"  ; morse["X"]="-..-" 
          morse["Y"]="-.--" ; morse["Z"]="--.." 
          morse["0"]="-----"; morse["1"]=".----"
          morse["2"]="..---"; morse["3"]=""
          morse["4"]="....-"; morse["5"]="....."
          morse["6"]="-...."; morse["7"]="--..."
          morse["8"]="---.."; morse["9"]="----."          
          while (1){
             print "1) English to Morse."
             print "2) Morse to English."
             print "3) Exit."
             printf "Enter choice: "
             getline which
             if (which==3) { exit }
             if (which==1 ){
                 printf "Enter letters to convert: "
                 getline choice
                 n=split(choice,arr,"")          
                 for(i=1;i<=n;i++) {
                     arr[i]=toupper(arr[i])
                     printf morse[arr[i]]              
                 }
                 print     
              }
              else if (which==2){
                  printf "Enter morse to convert: "
                  getline morsecode
                  for ( i in morse) {                       
                       if ( morse[i]==morsecode) {
                           print i
                       }
                  }
              
               }
             
             
             }
          

}

# 12  
Old 06-01-2007
Well, this is what the OP had posted

Quote:
Anyone can compete, and you could use any programming language you want, but since this is a C Programming Forum (well, it is Unix, but whatever), preferably C.

But trying in all possible ways should be good !
# 13  
Old 06-01-2007
was wondering whether this topic should have its own subforum or something, instead of in "C programming".
# 14  
Old 06-02-2007
Quote:
Originally Posted by ghostdog74
was wondering whether this topic should have its own subforum or something, instead of in "C programming".
No, we don't want to have too many forums that people get lost. But ideally people should not post in this forum inviting folks to program in a shell language. If the topic is that general, maybe the advanced forum would have been a better choice. But it's ok here. Sometimes we get a topic that does not nicely fall into any existing forum, but we can't create a new forum each time that happens.
Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. 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

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. 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
Login or Register to Ask a Question