Created
September 14, 2020 01:42
-
-
Save tivrfoa/8a30eeb88fb1c8ec3acba5a93b55e24e to your computer and use it in GitHub Desktop.
Checking if a string ends with another string in C
This file contains hidden or 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 <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; | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://stackoverflow.com/questions/10347689/how-can-i-check-whether-a-string-ends-with-csv-in-c