Skip to content

Instantly share code, notes, and snippets.

@tivrfoa
Created September 14, 2020 01:42
Show Gist options
  • Select an option

  • Save tivrfoa/8a30eeb88fb1c8ec3acba5a93b55e24e to your computer and use it in GitHub Desktop.

Select an option

Save tivrfoa/8a30eeb88fb1c8ec3acba5a93b55e24e to your computer and use it in GitHub Desktop.
Checking if a string ends with another string in C
#include <string.h>
#include <stdio.h>
int main() {
printf("%s", "/home/username/projects2/p2.geany\n");
char path[100] = "/home/username/projects2/p2.geany";
if (strstr(path, ".geany") != NULL) {
printf("path constains '.geany'\n");
} else {
printf("path DOES NOT constains '.geany': %s", path);
return -1;
}
char delim[2] = "/";
char *token = strtok(path, delim);
printf("token is %s\n", token);
char *final_token;
while (token != NULL)
{
printf("token is %s\n", token);
final_token = token;
token = strtok(NULL, delim);
}
printf("Final token is %s\n", final_token);
char *dot = strrchr(final_token, '.');
if (dot != NULL && strcmp(dot, ".geany") == 0) {
printf("token finishes with '.geany'\n");
} else {
printf("Invalid final token: %s\n", dot);
}
return 0;
}
@tivrfoa

tivrfoa commented Sep 14, 2020

Copy link
Copy Markdown
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment