How can i check whether a string ends with .csv in C

3 Answers

0 votes
#include <stdio.h>
#include <string.h>
 
int main() {
    char s[] = "salary.cvc";

	char *dot = strrchr(s, '.');
	
	if (dot && strcmp(dot, ".cvc") == 0)        
		puts("yes");
	else
		puts("no");
		
    return 0;
}


  
/*
run:
  
yes
  
*/

 



answered Nov 15, 2019 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
int main() {
    char s[] = "salary.cvc";
	
	char *dot = s + strlen(s) - 4;
	
	if (dot && strcmp(dot, ".cvc") == 0)        
        puts("yes");
    else
        puts("no");
		
    return 0;
}


  
/*
run:
  
yes
  
*/

 



answered Nov 15, 2019 by avibootz
0 votes
#include <stdio.h>
#include <string.h>
 
int main() {
    char s[] = "salary.cvc";
	int len = strlen(s);
	
	if (len > 4 && strcmp(s + len - 4, ".cvc") == 0)        
        puts("yes");
    else
        puts("no");
		
    return 0;
}


  
/*
run:
  
yes
  
*/

 



answered Nov 15, 2019 by avibootz
edited Nov 15, 2019 by avibootz
...