Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1100163
  • 博文数量: 165
  • 博客积分: 5957
  • 博客等级: 大校
  • 技术积分: 2015
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-24 15:04
文章分类

全部博文(165)

文章存档

2014年(10)

2013年(14)

2012年(9)

2011年(22)

2010年(17)

2009年(17)

2008年(26)

2007年(34)

2006年(16)

我的朋友

分类: C/C++

2009-03-17 17:39:56

1、随机数的使用 P162
 

#include <stdlib.h> /* For rand() and srand() */

time_t seed = 0; /* Seed value for random number sequence */
/* Set seed to be the number of seconds since Jan 1,1970 */
seed = time(NULL);
srand((unsigned int)seed); /* Initialize the random sequence */

/* Wait one second */

 
2、如何让程序等待1秒 P165
 

#include <time.h> /* For time() and clock() */

/* Stores current time - seed for random values */
time_t now = 0;
now = clock(); /* record start time for sequence */

/* Wait one second */
for( ;clock() - now < CLOCKS_PER_SEC; );

3、如何计算程序中的时长(单位秒)

#include <stdio.h>
#include <time.h>

int main(void)
{
        int time_taken = 0;
        long i = 0L;
        time_taken = clock();
        for (; i <= 1000000000; i++);
        time_taken = (clock() - time_taken) / CLOCKS_PER_SEC;
        printf("%d seconds\n", time_taken);
        return 0;
}

4、确定字符串的长度

#include <stdio.h>

int main(void)
{
        char str1[] = "To be or not to be";
        int count = 0; /* Stores the string length */
        while (str1[count]) /* Increment count till we reach the string, equal to: while (str1[count] != '\0') */
                count++; /* terminating character. */
        printf("\nThe length of the string \"%s\" is %d characters.", str1, count);

        return 0;
}

知道了这个原理,我们就可以使用strlen()了,为创建可移植性高的代码,应这样编写:

#include <stddef.h>

char str[] = "To be or not to be";
size_t count = 0;
count = strlen(str);

注意size_t在标准库头文件stddef.h中定义,是unsigned int。

5、字符串复制

strcpy()使用时不检查数组的大小,所以尽量使用strncpy()。

#include <string.h>
#include <stddef.h>

char distination[] = "This string will be replaced";
char source[] = "This string will be copied in part";
size_t n = 26;
strncpy(distination, source, n);

注意:source的长度如果大于n时,strncpy()就不会在目标字符串中添加‘\0’,这样是存在问题的。上例中distination最后的字符串形式是“This string will be copieded”。所以使用时要特别注意n>strlen(source).

6、字符串连接
strcat(str1, str2)将str2复制到str1的结尾并返回str1.
strncat(str1, str2, n)将str2中的前n个字符复制到str1的结尾并返回str1.
二者使用前请仔细确认str1的空间是否足够容纳复制后的字符串。
 
7、比较字符串
strcmp(str1, str2)返回一个小于、等于或大于0的int值。分别对应str1小于、等于和大于str2.

#include <stdio.h>

#include <string.h>

char str1[] = "The quick brown fox";
char str2[] = "The quick black fox";
if(strcmp(str1, str2) == 0)
        printf("str1 is equal to str2");

8、搜索字符串

(1)搜索字符串中的一个字符strchr(str, c)

    如果找到,返回找到的第一个给的字符的地址,如果没有找到,则返回NULL,相当于0

注意strchr的第二个参数是int型。所以下面的定义中是int定义。

    char *strchr(const char *s, int c);

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

char str[] = "The quick brown fox";
int c = 'q';
char *pGot_char = NULL;
pGot_char = strchr(str, c);
if(pGot_char != NULL)
        printf("Character found was %c.", *pGot_char);

    strrchr()与上一样,不过是从字符串末尾开始查找起。

(2)字符串中搜索子字符串

    strstr()是头文件声明的所有搜索函数中最有用的函数,它查找一个字符串中的子字符串,返回找到的第一个子字符串的位置指针。如果找不到就返回NULL。所以如果返回值不是NULL,就说明这个函数找到了所需的子字符串。

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


int main(void)
{
        char str1[] = "This string contains the holy grail.";
        char str2[] = "the holy grail";
        char str3[] = "the holy grill";
        /* Search str1 for the occurrence of str2 */
        if(strstr(str1, str2) == NULL)
                printf("\n\"%s\" was not found.", str2);
        else
                printf("\n\"%s\" was found in \"%s\"",str2, str1);
        /* Search str1 for the occurrence of str3 */
        if(strstr(str1, str3) == NULL)
                printf("\n\"%s\" was not found.", str3);
        else
                printf("\nWe shouldn't get to here!");


        printf("\n");
        return 0;
}

9、测试字符串

若需检查字符串内部的内容,可以使用头文件中的标准库函数

#include

int islower(int c); /* 小写字母 */

int isupper(int c); /* 大写字母 */

int isalpha(int c); /* 大写或小写字母 */

int isalnum(int c); /* 大写或小写字母或数字 */

int iscntrl(int c); /* 控制字符 */

int isprint(int c); /* 可打印字符,包括空格 */

int isgraph(int c); /* 可打印字符,不包括空格 */

int isdigit(int c); /* 十进制数字('0'~'9') */

int isxdigit(int c); /* 十六进制数字('0'~'9'、'A'~'F'、'a'~'f') */

int isblank(int c); /* 标准空白字符(空格、'\t')*/

int isspace(int c); /* 空白字符(空格、'\t'、'\n'、'\v'、'\r'、'\f') */

int ispunct(int c); /* isspace()和isalnum()返回false的可打印字符 */

如果这个单个字符在该函数测试范围内,所有以上函数均返回非0的int值,否则返回0

#include <stdio.h>
#include <ctype.h>


int main(void)
{
        char buffer[80]; /* Input buffer */
        int i = 0; /* Buffer index */
        int num_letters = 0; /* Number of letters in input */
        int num_digits = 0; /* Number of digits in input */
        printf("\nEnter an interesting string of less than 80 characters:\n");
        gets(buffer); /* Read a string into buffer */
        while(buffer[i] != '\0')
        {
                if(isalpha(buffer[i]))
                num_letters++; /* Increment letter count */
                if(isdigit(buffer[i++]))
                num_digits++; /* Increment digit count */
        }
        printf("\nYour string contained %d letters and %d digits.\n", num_letters, num_digits);
        return 0;
}

10、转换字符串

#include

int toupper(int c); /* 将小写字母转换为大写 */
int tolower(int c); /* 将大写字母转换为小写 */

2个函数都返回转换后的单个字符

结合toupper()和strstr()可以在一个字符串中忽略大小写查找另一字符串。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
        char text[100]; /* Input buffer for string to be searched */
        char substring[40]; /* Input buffer for string sought */
        printf("\nEnter the string to be searched (less than 100 characters):\n");
        fgets(text, sizeof(text), stdin);
        printf("\nEnter the string sought (less than 40 characters):\n");
        fgets(substring, sizeof(substring), stdin);


        /* overwrite the newline character in each string */
        text[strlen(text)-1] = '\0';
        substring[strlen(substring)-1] = '\0';
        printf("\nFirst string entered:\n%s\n", text);
        printf("\nSecond string entered:\n%s\n", substring);


        /* Convert both strings to uppercase. */
        for(int i = 0 ; (text[i] = toupper(text[i])) ; i++);
        for(int i = 0 ; (substring[i] = toupper(substring[i])) ; i++);
        printf("\nThe second string %s found in the first.",((strstr(text, substring) == NULL) ? "was not" : "was"));
        return 0;
}

将字符串转换为数值,头文件是stdlib.h

#include

double atof(const char *nptr); /* 从字符串参数中生产double类型的值 */

int atoi(const char *nptr); /* 从字符串参数中生成int类型的值 */
long atol(const char *nptr); /* 从字符串参数中生成long类型的值 */
long long atoll(const char *nptr);  /* 从字符串参数中生成long long类型的值 */

#include <stdlib.h>

char value_str = "8.4";
double value = 0;
value = atof(value_str);

 
11、宽字符串P226,这里略
阅读(1168) | 评论(0) | 转发(0) |
0

上一篇:10水果帮你远离春季病

下一篇:函数

给主人留下些什么吧!~~