<转>自:http://zhidao.baidu.com/question/173202165.html

首先就是memcpy
表头文件: #include <string.h>
定义函数: void *memcpy(void *dest, const void *src, size_t n)
函数说明: memcpy()用来拷贝src所指的内存内容前n个字节到dest所指的内存地址上。与strcpy()不同的是,memcpy()会完整的复制n个字节,不会因为遇到字符串结束'\0'而结束
返回值: 返回指向dest的指针
附加说明: 指针src和dest所指的内存区域不可重叠
例如:
你需要复制串str=“wangyucao1989”中的“yucao”,那么可以这么写:
memcpy(newstr,str+4,5); 除了memcpy之外,string还提供了strncpy函数:
函数名称: strncpy
函数原型: char *strncpy(char *dest, const char *src,int count)
函数功能: 将字符串src中的count个字符拷贝到字符串dest中去
函数返回: 指向dest的指针
参数说明: dest-目的字符串,src-源字符串,count-拷贝的字符个数
所属文档: <string.h> 还是上面的例子,这个程序可以这样写:
#include<stdio.h>
#include<string.h>
int main()
{
char str[] = "wangyucao1989"; char newstr[6];
//memcpy(newstr,str+4,5);
strncpy(newstr,str+4,5);
newstr[5] = '\0';
printf("%s\n",newstr);
return 0;
} ========================================================
位运算:
运算方法有六种: & 与运算
| 或运算
^ 异或运算
~ 非运算(求补)
>> 右移运算
<< 左移运算 运用这些基本的运算,我们可以解决acm所需的各种运算,给Bit赋1,赋0,给他的值取反,还有好多段操作。如下: 功能 | 示例 | 位运算
-----------------+---------------------+--------------------
去掉最后一位 | (101101->10110) | x >> 1
在最后加一个0 | (101101->1011010) | x < < 1
在最后加一个1 | (101101->1011011) | x < < 1+1
把最后一位变成1 | (101100->101101) | x | 1
把最后一位变成0 | (101101->101100) | x | 1-1
最后一位取反 | (101101->101100) | x ^ 1
把右数第k位变成1 | (101001->101101,k=3) | x | (1 < < (k-1))
把右数第k位变成0 | (101101->101001,k=3) | x & ~ (1 < < (k-1))
右数第k位取反 | (101001->101101,k=3) | x ^ (1 < < (k-1))
取末三位 | (1101101->101) | x & 7
取末k位 | (1101101->1101,k=5) | x & ((1 < < k)-1)
取右数第k位 | (1101101->1,k=4) | x >> (k-1) & 1
把末k位变成1 | (101001->101111,k=4) | x | (1 < < k-1)
末k位取反 | (101001->100110,k=4) | x ^ (1 < < k-1)
把右边连续的1变成0 | (100101111->100100000) | x & (x+1)
把右起第一个0变成1 | (100101111->100111111) | x | (x+1)
把右边连续的0变成1 | (11011000->11011111) | x | (x-1)
取右边连续的1 | (100101111->1111) | (x ^ (x+1)) >> 1
去掉右起第一个1的左边 | (100101000->1000) | x & (x ^ (x-1))
判断奇数 (x&1)==1
判断偶数 (x&1)==0
取右边第一个1所在位置 x&-x ================================================================
类型转换:
函数名: abs 功 能: 求整数的绝对值
用 法: int abs(int i);
程序例:
#include <stdio.h>
#include <math.h> int main(void)
{
int number = -1234; printf("number: %d absolute value: %d\n", number, abs(number));
return 0;
}
函数名: atof
功 能: 把字符串转换成浮点数
用 法: double atof(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h> int main(void)
{
float f;
char *str = "12345.67"; f = atof(str);
printf("string = %s float = %f\n", str, f);
return 0;
} 函数名: atoi
功 能: 把字符串转换成长整型数
用 法: int atoi(const char *nptr);
程序例:
#include <stdlib.h>
#include <stdio.h> int main(void)
{
int n;
char *str = "12345.67"; n = atoi(str);
printf("string = %s integer = %d\n", str, n);
return 0;
} 函数名: atol
功 能: 把字符串转换成长整型数
用 法: long atol(const char *nptr);
程序例: #include <stdlib.h>
#include <stdio.h> int main(void)
{
long l;
char *str = "98765432"; l = atol(lstr);
printf("string = %s integer = %ld\n", str, l);
return(0);
} ===========================================================
其他函数:
函数名: bsearch
功 能: 二分法搜索
用 法: void *bsearch(const void *key, const void *base, size_t *nelem, size_t width, int(*fcmp)(const void *, const *));
程序例: #include <stdlib.h>
#include <stdio.h> #define NELEMS(arr) (sizeof(arr) / sizeof(arr[0])) int numarray[] = {123, 145, 512, 627, 800, 933}; int numeric (const int *p1, const int *p2)
{
return(*p1 - *p2);
} int lookup(int key)
{
int *itemptr; /* The cast of (int(*)(const void *,const void*))
is needed to avoid a type mismatch error at
compile time */
itemptr = bsearch (&key, numarray, NELEMS(numarray),
sizeof(int), (int(*)(const void *,const void *))numeric);
return (itemptr != NULL);
} int main(void)
{
if (lookup(512))
printf("512 is in the table.\n");
else
printf("512 isn't in the table.\n"); return 0;
}
函数名: fabs
功 能: 返回浮点数的绝对值
用 法: double fabs(double x);
程序例: #include <stdio.h>
#include <math.h> int main(void)
{
float number = -1234.0; printf("number: %f absolute value: %f\n",
number, fabs(number));
return 0;
} 函数名: fcvt
功 能: 把一个浮点数转换为字符串
用 法: char *fcvt(double value, int ndigit, int *decpt, int *sign);
程序例: #include <stdlib.h>
#include <stdio.h>
#include <conio.h> int main(void)
{
char *string;
double value;
int dec, sign;
int ndig = 10; clrscr();
value = 9.876;
string = ecvt(value, ndig, &dec, &sign);
printf("string = %s dec = %d \
sign = %d\n", string, dec, sign); value = -123.45;
ndig= 15;
string = ecvt(value,ndig,&dec,&sign);
printf("string = %s dec = %d sign = %d\n",
string, dec, sign); value = 0.6789e5; /* scientific
notation */
ndig = 5;
string = ecvt(value,ndig,&dec,&sign);
printf("string = %s dec = %d\
sign = %d\n", string, dec, sign); return 0;
} 函数名: gcvt
功 能: 把浮点数转换成字符串
用 法: char *gcvt(double value, int ndigit, char *buf);
程序例: #include <stdlib.h>
#include <stdio.h> int main(void)
{
char str[25];
double num;
int sig = 5; /* significant digits */ /* a regular number */
num = 9.876;
gcvt(num, sig, str);
printf("string = %s\n", str); /* a negative number */
num = -123.4567;
gcvt(num, sig, str);
printf("string = %s\n", str); /* scientific notation */
num = 0.678e5;
gcvt(num, sig, str);
printf("string = %s\n", str); return(0);
}
函数名: itoa
功 能: 把一整数转换为字符串
用 法: char *itoa(int value, char *string, int radix);
程序例: #include <stdlib.h>
#include <stdio.h> int main(void)
{
int number = 12345;
char string[25]; itoa(number, string, 10);
printf("integer = %d string = %s\n", number, string);
return 0;
} 函数名: labs
功 能: 取长整型绝对值
用 法: long labs(long n);
程序例: #include <stdio.h>
#include <math.h> int main(void)
{
long result;
long x = -12345678L; result= labs(x);
printf("number: %ld abs value: %ld\n",
x, result); return 0;
}
函数名: memcpy
功 能: 从源source中拷贝n个字节到目标destin中
用 法: void *memcpy(void *destin, void *source, unsigned n);
程序例: #include <stdio.h>
#include <string.h>
int main(void)
{
char src[] = "******************************";
char dest[] = "abcdefghijlkmnopqrstuvwxyz0123456709";
char *ptr;
printf("destination before memcpy: %s\n", dest);
ptr = memcpy(dest, src, strlen(src));
if (ptr)
printf("destination after memcpy: %s\n", dest);
else
printf("memcpy failed\n");
return 0;
}
函数名: memset
功 能: 设置s中的所有字节为ch, s数组的大小由n给定
用 法: void *memset(void *s, char ch, unsigned n);
程序例: #include <string.h>
#include <stdio.h>
#include <mem.h> int main(void)
{
char buffer[] = "Hello world\n"; printf("Buffer before memset: %s\n", buffer);
memset(buffer, '*', strlen(buffer) - 1);
printf("Buffer after memset: %s\n", buffer);
return 0;
}
函数名: pow 功 能: 指数函数(x的y次方)
用 法: double pow(double x, double y);
程序例: #include <math.h>
#include <stdio.h> int main(void)
{
double x = 2.0, y = 3.0; printf("%lf raised to %lf is %lf\n", x, y, pow(x, y));
return 0;
} 函数名: qsort
功 能: 使用快速排序例程进行排序
用 法: void qsort(void *base, int nelem, int width, int (*fcmp)());
程序例: #include <stdio.h>
#include <stdlib.h>
#include <string.h> int sort_function( const void *a, const void *b); char list[5][4] = { "cat", "car", "cab", "cap", "can" }; int main(void)
{
int x; qsort((void *)list, 5, sizeof(list[0]), sort_function);
for (x = 0; x < 5; x++)
printf("%s\n", list[x]);
return 0;
} int sort_function( const void *a, const void *b)
{
return( strcmp(a,b) );
} 函数名: sqrt
功 能: 计算平方根
用 法: double sqrt(double x);
程序例: #include <math.h>
#include <stdio.h> int main(void)
{
double x = 4.0, result; result = sqrt(x);
printf("The square root of %lf is %lf\n", x, result);
return 0;
}
======================================================== 字符串函数:
函数名: sscanf
功 能: 执行从字符串中的格式化输入
用 法: int sscanf(char *string, char *format[,argument,...]);
程序例: #include <stdio.h>
#include <conio.h> int main(void)
{
char label[20];
char name[20];
int entries = 0;
int loop, age;
double salary; struct Entry_struct
{
char name[20];
int age;
float salary;
} entry[20]; /* Input a label as a string of characters restricting to 20 characters */
printf("\n\nPlease enter a label for the chart: ");
scanf("%20s", label);
fflush(stdin); /* flush the input stream in case of bad input */ /* Input number of entries as an integer */
printf("How many entries will there be? (less than 20) ");
scanf("%d", &entries);
fflush(stdin); /* flush the input stream in case of bad input */ /* input a name restricting input to only letters upper or lower case */
for (loop=0;loop<entries;++loop)
{
printf("Entry %d\n", loop);
printf(" Name : ");
scanf("%[A-Za-z]", entry[loop].name);
fflush(stdin); /* flush the input stream in case of bad input */ /* input an age as an integer */
printf(" Age : ");
scanf("%d", &entry[loop].age);
fflush(stdin); /* flush the input stream in case of bad input */ /* input a salary as a float */
printf(" Salary : ");
scanf("%f", &entry[loop].salary);
fflush(stdin); /* flush the input stream in case of bad input */
} /* Input a name, age and salary as a string, integer, and double */
printf("\nPlease enter your name, age and salary\n");
scanf("%20s %d %lf", name, &age, &salary); /* Print out the data that was input */
printf("\n\nTable %s\n",label);
printf("Compiled by %s age %d $%15.2lf\n", name, age, salary);
printf("-----------------------------------------------------\n");
for (loop=0;loop<entries;++loop)
printf("%4d | %-20s | %5d | %15.2lf\n",
loop + 1,
entry[loop].name,
entry[loop].age,
entry[loop].salary);
printf("-----------------------------------------------------\n");
return 0;
}
函数名: stpcpy
功 能: 拷贝一个字符串到另一个
用 法: char *stpcpy(char *destin, char *source);
程序例: #include <stdio.h>
#include <string.h> int main(void)
{
char string[10];
char *str1 = "abcdefghi"; stpcpy(string, str1);
printf("%s\n", string);
return 0;
} 函数名: strcat
功 能: 字符串拼接函数
用 法: char *strcat(char *destin, char *source);
程序例: #include <string.h>
#include <stdio.h> int main(void)
{
char destination[25];
char *blank = " ", *c = "C++", *Borland = "Borland"; strcpy(destination, Borland);
strcat(destination, blank);
strcat(destination, c); printf("%s\n", destination);
return 0;
} 函数名: strchr
功 能: 在一个串中查找给定字符的第一个匹配之处\
用 法: char *strchr(char *str, char c);
程序例: #include <string.h>
#include <stdio.h> int main(void)
{
char string[15];
char *ptr, c = 'r'; strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
printf("The character %c is at position: %d\n", c, ptr-string);
else
printf("The character was not found\n");
return 0;
} 函数名: strcmp
功 能: 串比较
用 法: int strcmp(char *str1, char *str2);
程序例: #include <string.h>
#include <stdio.h> int main(void)
{
char *buf1 = "aaa", *buf2 = "bbb", *buf3 = "ccc";
int ptr; ptr = strcmp(buf2, buf1);
if (ptr > 0)
printf("buffer 2 is greater than buffer 1\n");
else
printf("buffer 2 is less than buffer 1\n"); ptr = strcmp(buf2, buf3);
if (ptr > 0)
printf("buffer 2 is greater than buffer 3\n");
else
printf("buffer 2 is less than buffer 3\n"); return 0;
}
函数名: strcpy
功 能: 串拷贝用 法: char *strcpy(char *str1, char *str2);
程序例: #include <stdio.h>
#include <string.h> int main(void)
{
char string[10];
char *str1 = "abcdefghi"; strcpy(string, str1);
printf("%s\n", string);
return 0;
} 函数名: strrev
功 能: 串倒转用 法: char *strrev(char *str);
程序例: #include <string.h>
#include <stdio.h> int main(void)
{
char *forward = "string"; printf("Before strrev(): %s\n", forward);
strrev(forward);
printf("After strrev(): %s\n", forward);
return 0;
}
函数名: strset
功 能: 将一个串中的所有字符都设为指定字符
用 法: char *strset(char *str, char c);
程序例: #include <stdio.h>
#include <string.h> int main(void)
{
char string[10] = "123456789";
char symbol = 'c'; printf("Before strset(): %s\n", string);
strset(string, symbol);
printf("After strset(): %s\n", string);
return 0;
} 函数名: strstr
功 能: 在串中查找指定字符串的第一次出现
用 法: char *strstr(char *str1, char *str2);
程序例: #include <stdio.h>
#include <string.h> int main(void)
{
char *str1 = "Borland International", *str2 = "nation", *ptr; ptr = strstr(str1, str2);
printf("The substring is: %s\n", ptr);
return 0;
} 函数名: strtod
功 能: 将字符串转换为double型值
用 法: double strtod(char *str, char **endptr);
程序例: #include <stdio.h>
#include <stdlib.h> int main(void)
{
char input[80], *endptr;
double value; printf("Enter a floating point number:");
gets(input);
value = strtod(input, &endptr);
printf("The string is %s the number is %lf\n", input, value);
return 0;
}
函数名: strtol
功 能: 将串转换为长整数
用 法: long strtol(char *str, char **endptr, int base);
程序例: #include <stdlib.h>
#include <stdio.h> int main(void)
{
char *string = "87654321", *endptr;
long lnumber; /* strtol converts string to long integer */
lnumber = strtol(string, &endptr, 10);
printf("string = %s long = %ld\n", string, lnumber); return 0;
} 函数名: strupr
功 能: 将串中的小写字母转换为大写字母
用 法: char *strupr(char *str);
程序例: #include <stdio.h>
#include <string.h> int main(void)
{
char *string = "abcdefghijklmnopqrstuvwxyz", *ptr; /* converts string to upper case characters */ ptr = strupr(string);
printf("%s\n", ptr);
return 0;
} 函数名: tolower
功 能: 把字符转换成小写字母
用 法: int tolower(int c);
程序例: #include <string.h>
#include <stdio.h>
#include <ctype.h> int main(void)
{
int length, i;
char *string = "THIS IS A STRING"; length = strlen(string);
for (i=0; i<length; i++)
{
string[i] = tolower(string[i]);
}
printf("%s\n",string); return 0;
} 函数名: toupper
功 能: 把字符转换成大写字母
用 法: int toupper(int c);
程序例: #include <string.h>
#include <stdio.h>
#include <ctype.h> int main(void)
{
int length, i;
char *string = "this is a string"; length = strlen(string);
for (i=0; i<length; i++)
{
string[i] = toupper(string[i]);
} printf("%s\n",string); return 0;
}

C++ 字符串相关函数的更多相关文章

  1. Perl函数:字符串相关函数

    Perl字符串相关函数 字符串的内置函数有: chomp, chop, chr, crypt, fc, hex, index, lc, lcfirst, length, oct, ord, pack, ...

  2. db2字符串相关函数的使用

    db2字符串相关函数的使用 from :internet    一.字符转换函数 1.ASCII() 返回字符表达式最左端字符的ASCII 码值.在ASCII()函数中,纯数字的字符串可不用‘’括起来 ...

  3. 009.Python字符串相关函数

    字符串相关函数 1 capitalize 字符串首字母大写 strvar = "this is a dog" res = strvar.capitalize() print(res ...

  4. Oracle学习笔记_09_字符串相关函数

    二.参考资料 0.Oracle中的字符串类型及相关函数详解 1.ORACLE 字符串操作 2.oracle函数大全-字符串处理函数

  5. JMeter常用字符串相关函数

    JMeter的惯用函数使用-字符串相关 主要的函数如下:1.将字符串转为大写或小写: ${__lowercase(Hello,)}  ${__uppercase(Hello,)}2.生成字符串:  _ ...

  6. python3 字符串相关函数

    python版本 3.5 #Author by Liguangbo#_*_ coding:utf-8 _*_str="i like study python, welcome to my p ...

  7. sqlserver 字符串相关函数

    http://www.cnblogs.com/jiajiayuan/archive/2011/06/16/2082488.html 以下所有例子均Studnet表为例:  计算字符串长度len()用来 ...

  8. MySQL字符串相关函数学习二

    ① LOWER(str):将字符串转为小写:与此函数具有相同作用的函数有LCASE() 如果参数是小写.数字或其他特殊字符,则返回原数据 ② LEFT(str, len):返回字符串str左边的len ...

  9. MySQL字符串相关函数学习一

    这里总结一下常用的或者有可能用到的一些字符串内建函数 ① ASCII() :返回字符的ASCII码 如果输入的不是一个字符而是一个字符串呢?ascii()会只取第一个字符作为计算的参数,如: ② CH ...

随机推荐

  1. PHP中正则替换函数preg_replace用法笔记

    今天应老板的需求,需要将不是我们的页面修改一个链接,用js+iframe应该也能实现,但是我想尝试一下php实现方法. 首先你得先把别人的页面download到你的php中,实现方法可以用curl, ...

  2. Oracle数据库作业-4 查询

    9. 查询"95031"班的学生人数.

  3. IntelliJ IDEA 14.0.2破解注册码文件(2015年06月08日)

    Java文件代码: import java.math.BigInteger;import java.util.Date;import java.util.Random;import java.util ...

  4. 学习Slim Framework for PHP v3 ( 二)

    昨天说到能够成功将本地的URL通过在index.php 中添加get(pattern,clouser)路由到指定的处理类中,处理后(这里指存入数据库中),然后返回response在浏览器中显示. 昨天 ...

  5. Android&iOS视频直播之旅

    现在的移动互联网时代,大家的网速真是越来越快,高带宽的WIFI和覆盖率极大的4G,4G+把手机观看视频直播推上了风口浪尖,越来越多的应用在玩手机视频直播,我们做的应用里也要嵌入视频直播. 这篇文章里我 ...

  6. Part 10 AngularJS sort rows by table header

    Here is what we want to do 1. The data should be sorted when the table column header is clicked 2. T ...

  7. linux中Apache更Nginx环境配置教程

    想把Apache2.2换Nginx已经有些日子,今天给动手了.找了个稳定版本:1.4.1  http://nginx.org/download/nginx-1.4.1.zip 配置很简单,就是ngin ...

  8. cocos2d-x 创建工程详解

    我们的编写的第一个程序一般习惯上都命名为HelloWorld,从它开始再学习其他的内容.下面介绍的第一个Cocos2d-x游戏我们也命名为HelloWorld. 创建工程 在Cocos2d-x早期版本 ...

  9. PHP的接口(interface)

    接口声明了函数和字段,但不会给出实现的细节 规则: 1.类全部为抽象方法(不需要声明abstract) 2.接口抽象方法必须是public 3.成员(字段)必须是常量 interface Comput ...

  10. 一个小makefile程序

    刚刚开始学习linux下的程序,总需要自己写一些小型的makefile文件,这里给出一个makefile的例子,方便以后借鉴. 程序包含了main.c(需要头文件input.h 和 display.h ...