-
-
Save CraigRodrigues/54d2afd0195957ee770825886f8837b7 to your computer and use it in GitHub Desktop.
CS50x Coding Contest 2016 Practice - Substring Challenge
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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