Comment on: We need more programming challenges. We should start off small: First non-repeating character of a string. Any language you like.
1 16 Apr 2016 15:20 u/MadCamel in v/programmingComment on: We need more programming challenges. We should start off small: First non-repeating character of a string. Any language you like.
Perl:
#!/usr/bin/perl -n
s/(.)(\1)+//; printf("%s\n", (split(//))[0]);
C. Pointer math is fun.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
char buf[512], *p, fc;
while (read(0, &buf, 512) > 0) {
for (p=buf, fc = *p; *p == fc && *p != 0; p++);
if (p-1 == buf) // Single character edge-case
printf("%c\n", fc);
else
printf("%c\n", *p);
}
}
Thanks for giving it a try! I thought I was completely buried down here. That's proper behaviour given the specification as I understand it.
If I were asked to re-phrase the specification: Print the first character that isn't a repeat of the last.