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

计算字符串的字符数和查找几个字符串中是否含有特定字符

时间:2015-08-20 15:15:26      阅读:148      评论:0      收藏:0      [点我收藏+]

标签:

代码来自于《C与指针》关于指针的介绍

知识点:指针数组越界

  #define NULL   (void*)0

  代码量的积累很重要!

  代码量的积累很重要!

  代码量的积累很重要!

 

#include<stdio.h>
//#include<stdlib.h>
#include<assert.h>

char str[] = "ABCDEFG";
size_t str_len(char *string);
int str_find(char **strings,char value);
char *strs[6]={"red","yellow","blue","white","black",NULL};

int main()
{
    int tmp;
    tmp = str_len(str);
    char ch;    
    printf(" NULL %d\n",NULL);
    //printf(" NULL %d\n",&NULL);   ERR
    printf("%d\n",tmp);
    printf("%d\n",sizeof(strs[0]));

    for(ch=a;ch<z;ch++)
        str_find(strs,ch);
    
    return 0;
    
}
//查找几个字符串中是否含有某个字符 找到返回1没有返回0
int str_find(char **strings,char value)
{
    char *string;  //当前正在查找的字符串
    char *stringIn;
    
    while( (string=*strings++) != NULL )    //while( (string=*strings++) == NULL )  粗心!!! 字符串数组越界!!?
    {
        assert(strings != NULL);        //断言 异常处理
        stringIn = string;
        while( *string !=\0 )
        {
            if( *string++ == value)      //if(*string==value)   粗心!!!
            {
                printf("%c is in the %s\n",value,stringIn);
                return 1;
            }
        }
    }
    printf("can‘t find  %c \n",value);
    return 0;
}

//计算字符串的长度
size_t str_len(char *string)
{
    int length = 0;
    while(*string++ != \0)
        length++;
    return length;
}
/*
E:\myDocuments\GUN\sources>gcc Point.c

E:\myDocuments\GUN\sources>a
 NULL 0
7
4
a is in the black
b is in the blue
c is in the black
d is in the red
e is in the red
can‘t find  f
can‘t find  g
h is in the white
i is in the white
can‘t find  j
k is in the black
l is in the yellow
can‘t find  m
can‘t find  n
o is in the yellow
can‘t find  p
can‘t find  q
r is in the red
can‘t find  s
t is in the white
u is in the blue
can‘t find  v
w is in the yellow
can‘t find  x
y is in the yellow

E:\myDocuments\GUN\sources>
*/

 

计算字符串的字符数和查找几个字符串中是否含有特定字符

标签:

原文地址:http://www.cnblogs.com/hinice/p/4744986.html

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