Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/catname.c Secret
Created Jul 30, 2016

Embed
What would you like to do?
CS50x Coding Contest 2016 - Name Your Cat
#include <stdio.h>
#include <string.h>
#include "cs50.h"
int main(void)
{
int n = GetInt();
string s = GetString();
int i = 0;
int j = 1;
while (i < (n-j))
{
if (s[i] == 'a' || s[i] == 'c' || s[i] == 'e' || s[i] == 'i' || s[i] == 'k' || s[i] == 'n' || s[i] == 't')
{
if (s[n-j] == 'a' || s[n-j] == 'c' || s[n-j] == 'e' || s[n-j] == 'i' || s[n-j] == 'k' || s[n-j] == 'n' || s[n-j] == 't')
{
//swap
char temp = s[i];
s[i] = s[n-j];
s[n-j] = temp;
i++;
j++;
}
else
{
//incrememnt j if we don't swap to find next viable char
j++;
}
}
else
i++;
}
printf("%s", s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.