Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues CraigRodrigues/substring.c Secret
Created Jul 28, 2016

Embed
What would you like to do?
CS50x Coding Contest 2016 Practice - Substring Challenge
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cs50.h"
int main(void)
{
char* input = GetString();
char* substring = GetString();
int location = 0;
int i = 0;
int j = 0;
while (input[i] != '\0')
{
// either the characters match here or if the wildcard is used incrememnt both strings and compare again
while (input[i] == substring[j] || substring[j] == '*')
{
i++;
j++;
}
if (substring[j] == '\0')
{
printf("%i", location);
return 0;
}
location++;
i++;
j = 0; // need to reset the substring back to the first character if we reach this far
}
printf("-1");
return 1;
}
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.