Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Created July 28, 2016 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CraigRodrigues/54d2afd0195957ee770825886f8837b7 to your computer and use it in GitHub Desktop.
Save CraigRodrigues/54d2afd0195957ee770825886f8837b7 to your computer and use it in GitHub Desktop.
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