Skip to content

Instantly share code, notes, and snippets.

@tudorconstantin
Created March 4, 2015 15:54
Show Gist options
  • Save tudorconstantin/10aac51d6e9210a64672 to your computer and use it in GitHub Desktop.
Save tudorconstantin/10aac51d6e9210a64672 to your computer and use it in GitHub Desktop.
Find string in string (equivalent of Perl's index core function)
include <stdio.h>
int main(void)
{
char name[3] = "Bob";
char name1[10];
int i = 0;
int found_at_position = -1;
printf("Give me a name pls\n");
scanf(" %s", name1);
//TODO: fix this for
for(i=0;name[i]==name1[i]; i++)
{
//TODO: set the found_at_position variable to the value where name starts in name1
printf(" %s", name1);
}
if ( found_at_position > -1 ){
printf("String |%s| is found in string |%s| at position %d\n", name, name1, found_at_position);
} else {
printf("String |%s| does not contain string |%s|\n", name1, name );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment