2012年6月20日 星期三

[C] 從指定之位置比較兩字串



/*
 * Program: Complement header file of string compare 
 * Language:GNU C/ ANSI C
 * Author:  Veck Hsiao @ Taiwan, National Chung Cheng University
 * Time:    June/21/2012 
 * Usage:   Compare string 1 with string 2 start from assigned index
 * Result:  If matched, return 0, otherwise, return -1
 */

#include<stdio.h>      
#include<stdlib.h>
#include<string.h>

int strmidcmp(const char *str1, const char *str2, int start) 
{
    int i=0, j=0;
    
    for(i=start, j=0; j<strlen(str2); i++, j++)    
     if(str1[i]==str2[j])
       continue;
     else
       return -1;
    
     return 0;
    
}



Usage:
    printf("%d\n",strmidcmp("middle","ddlejd",2));    //-1
    printf("%d\n",strmidcmp("middle","dd",2));        //0
    printf("%d\n",strmidcmp("middle","add",2));       //-1

沒有留言:

張貼留言