Skip to content

Instantly share code, notes, and snippets.

@giorgi568
Created August 4, 2024 10:56
Show Gist options
  • Select an option

  • Save giorgi568/37bc8e4f9416a26b78c13e02d6012bdf to your computer and use it in GitHub Desktop.

Select an option

Save giorgi568/37bc8e4f9416a26b78c13e02d6012bdf to your computer and use it in GitHub Desktop.
the c programming language book, exercise 2-5
#include <stdio.h>
/*
this is supposed to return first location in
string s1 where any character from the string
s2 occurs, or -1 if no such occurances exist.
*/
int any(char s1[], char s2[]);
int any(char s1[], char s2[]){
int i, l;
for(i = 0; s1[i] != '\0'; i++){
for(l = 0; s2[l] != '\0'; l++){
if(s1[i] == s2[l]){
return i;
}
}
}
return -1;
}
void main(){
char s1[] = "car";
char s2[] = "r";
printf("%d\n", any(s1, s2));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment