码迷,mamicode.com
首页 > 其他好文 > 详细

Get string middle by C

时间:2015-04-04 09:06:34      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:

Key:

void str_get_middlle( char *result, char *str, int start, int end ){
    
    int i;
    int c;

    for( str += start, i = 0; (start + i ) <= end && (*result++ = *str++) != \0‘; i++ ){
                ;
    }

}

For example:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5 #define STR_LEN 10
 6 
 7 void str_get_middlle( char *result, char *str, int start, int end );
 8 
 9 int main( int argc, char *argv[] )
10 {
11     FILE *fp = NULL;
12     char *file_path = NULL;
13     char str[STR_LEN] = "", result[ STR_LEN ] = "";
14 
15     if( argc == 1){
16         printf( "Too few augument(s)\n" );
17         exit( EXIT_FAILURE );
18     }
19 
20     file_path = *(++argv);
21 
22     if( (fp = fopen( file_path , "r" )) == NULL ){
23         printf( "Opening file error! path:%s\n", file_path );
24         exit( EXIT_FAILURE );
25     }
26     else{
27         printf( "Opening file [%s] was successfully!\n", file_path );
28     }
29 
30 
31     while( fgets( str, sizeof( str ),fp) != NULL ){
32 //        printf( "%s", str );    
33         str_get_middlle( result, str, 0, 1);
34         printf( "%s-", result );
35 
36         str_get_middlle( result, str, 2, 3);
37         printf( "%s-", result );
38 
39         str_get_middlle( result, str, 4, 5);
40         printf( "%s\n", result );
41     } 
42     
43     return 0;
44 }

  Means: 00112233 >> 00-11-22-33.  This code can formt mac address.

Get string middle by C

标签:

原文地址:http://www.cnblogs.com/LonelyThinker/p/4391557.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!