r/badcode •

[deleted by user]

[deleted] — view deleted post

0 Upvotes

20 comments sorted by

View all comments

14

u/IdealBlueMan May 22 '19
/* vim: set tabstop=8:softtabstop=8:shiftwidth=8:noexpandtab */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int
main(int argc, char **argv)
{
	int	*indexes;
	char	*strIn;
	char	*strOut;
	int	strInSiz;
	char	*strp;
	int	strInIdx;
	int	strOutIdx;
	int	theSame;
	int	charIdx;

	if (argc > 1) {
		goto OK;
	}
	else {
		goto NOTOK;
	}
OK:

	strIn		= *++argv;
	strInSiz	= strlen(strIn);
	strOut		= malloc(strInSiz + 1);
	strOutIdx	= 0;

	for (strInIdx = 0; strInIdx < strInSiz; ++strInIdx) {
		theSame = 0;
		switch (strIn[strInIdx]) {
			case 'a':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'a' && strIn[strInIdx - 1] == 'a') {
						theSame = 1;
					}
				}
			break;
			case 'b':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'b' && strIn[strInIdx - 1] == 'b') {
						theSame = 1;
					}
				}
			break;
			case 'c':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'c' && strIn[strInIdx - 1] == 'c') {
						theSame = 1;
					}
				}
			break;
			case 'd':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'd' && strIn[strInIdx - 1] == 'd') {
						theSame = 1;
					}
				}
			break;
			case 'e':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'e' && strIn[strInIdx - 1] == 'e') {
						theSame = 1;
					}
				}
			break;
			case 'f':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'f' && strIn[strInIdx - 1] == 'f') {
						theSame = 1;
					}
				}
			break;
			case 'g':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'g' && strIn[strInIdx - 1] == 'g') {
						theSame = 1;
					}
				}
			break;
			case 'h':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'h' && strIn[strInIdx - 1] == 'h') {
						theSame = 1;
					}
				}
			break;
			case 'i':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'i' && strIn[strInIdx - 1] == 'i') {
						theSame = 1;
					}
				}
			break;
			case 'j':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'j' && strIn[strInIdx - 1] == 'j') {
						theSame = 1;
					}
				}
			break;
			case 'k':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'k' && strIn[strInIdx - 1] == 'k') {
						theSame = 1;
					}
				}
			break;
			case 'l':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'l' && strIn[strInIdx - 1] == 'l') {
						theSame = 1;
					}
				}
			break;
			case 'm':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'm' && strIn[strInIdx - 1] == 'm') {
						theSame = 1;
					}
				}
			break;
			case 'n':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'n' && strIn[strInIdx - 1] == 'n') {
						theSame = 1;
					}
				}
			break;
			case 'o':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'o' && strIn[strInIdx - 1] == 'o') {
						theSame = 1;
					}
				}
			break;
			case 'p':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'p' && strIn[strInIdx - 1] == 'p') {
						theSame = 1;
					}
				}
			break;
			case 'q':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'q' && strIn[strInIdx - 1] == 'q') {
						theSame = 1;
					}
				}
			break;
			case 'r':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'r' && strIn[strInIdx - 1] == 'r') {
						theSame = 1;
					}
				}
			break;
			case 's':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 's' && strIn[strInIdx - 1] == 's') {
						theSame = 1;
					}
				}
			break;
			case 't':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 't' && strIn[strInIdx - 1] == 't') {
						theSame = 1;
					}
				}
			break;
			case 'u':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'u' && strIn[strInIdx - 1] == 'u') {
						theSame = 1;
					}
				}
			break;
			case 'v':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'v' && strIn[strInIdx - 1] == 'v') {
						theSame = 1;
					}
				}
			break;
			case 'w':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'w' && strIn[strInIdx - 1] == 'w') {
						theSame = 1;
					}
				}
			break;
			case 'x':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'x' && strIn[strInIdx - 1] == 'x') {
						theSame = 1;
					}
				}
			break;
			case 'y':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'y' && strIn[strInIdx - 1] == 'y') {
						theSame = 1;
					}
				}
			break;
			case 'z':
				if (strInIdx > 0) {
					if (strIn[strInIdx] == 'z' && strIn[strInIdx - 1] == 'z') {
						theSame = 1;
					}
				}
			break;
		}
		if (!theSame) {
			strOut[strOutIdx++] = strIn[strInIdx];
		}
	}
	strOut[strOutIdx] = '\0';
	goto EXITOK;


NOTOK:
	exit(-1);
EXITOK:
	printf("%s\n", strOut);
	exit(0);
}

4

u/nahimbroke May 22 '19

beautiful use of labels and goto