#include "stdafx.h" #include "stdio.h" #include <cstring> int main(int argc, char* argv[]) { //char d[] = "123"; char s[] = "123456789"; char d[] = "123"; strcpy(d,s); printf("%s,\n%s",d,s); printf("Hello World!\n"); return 0; }
上述执行结果为:123456789,56789.分配的内存地址是连续内存。原来是123\0123456789\0,strcpy后变成了123456789\06789\0.如果把目标字串定义在前,源字符串定义在后,则产生运行期错误。访问到了不可预知的地址。
字符串拷贝函数实现
void stringcpy(char *to, const char *from){ assert(to != NULL && from != NULL); while(*from != '\0'){ *to++ = *from++; } *to = '\0'; }
int strcmp (const char * src, const char * dst) { int ret = 0 ; while( ! (ret = *(unsigned char *)src - *(unsigned char *)dst) && *src) ++src, ++dst; if ( ret < 0 ) ret = -1 ; else if ( ret > 0 ) ret = 1 ; return( ret ); }
字符串比较
查找子字符串在母字符串出现的位置,返回地址值。
char * findsub(char * mother, char * son) { for(int i = 0; i < strlen(mother); i++) { char * temp_mother = mother + i; char * temp_son = son; while( (* temp_mother) == (*temp_son) && ( (*temp_son) != '\0')) { temp_mother++; temp_son++; } if ( (*temp_son) == '\0') { return mother + i; } } return NULL; }
翻转单词:
void reverse_word() { char a[] = "I love you"; char b[] = "11"; printf("strlen b is %d\n",strlen(b));//strlen不包含最后的'\0'结束符, //这点和sizeof 不一样。 int end = strlen(a) - 1; int start = 0; while(start <= end) //将整个字符串翻转, { char temp = a[start]; a[start] = a[end]; a[end] = temp; start++; end--; } printf("after reverse is %s\n",a);//再逐个单词翻转。 start = 0; while(a[start] == ' ') start++; while(a[start] != '\0') { int end = start; while(a[end] != ' ' && (a[end] != '\0')) //找到第一个单词结束位置 end++; end--; printf("end is %d\n",end); int tempend = end + 1; while(start <= end) { char temp = a[start]; a[start] = a[end]; a[end] = temp; start++; end--; } while(a[tempend] == ' ' && (a[tempend] != '\0')) tempend++; start = tempend; } printf("a is %s\n",a); }
发表评论
-
gcc gdb常用命令
2010-10-06 11:20 1098gdb 链接: http://fanqiang.chinau ... -
指针数组,数组指针
2010-10-02 14:40 699void test(char* a[]) { ... -
如何用栈实现递归与非递归的转换
2010-04-10 15:18 931http://bbs.chinaunix.net/viewth ... -
华为笔试2
2009-06-16 09:37 964【转】http://hi.baidu.com/xiao1dia ... -
华为笔试1
2009-06-16 09:35 1001【转】http://hi.baidu.com/xi ... -
用两个栈实现一个队列的功能
2009-06-03 15:22 2560【转】http://hi.baidu.com/liangjw8 ... -
嵌入式程序员应该知道的16个问题
2009-05-29 15:41 1025【转】http://blog.csdn.net/s ... -
嵌入式程序员应该知道的16个问题
2009-05-29 15:39 1480【转】http://blog.csdn.net/seraphs ... -
嵌入式程序员应该知道的16个问题
2009-05-29 15:38 856【转】http://blog.csdn.net/s ... -
嵌入式程序员应该知道的16个问题
2009-05-29 15:37 860【转】http://blog.csdn.net/s ... -
c预编译 #define相关
2009-05-29 15:14 1177#是生成字符串: #define a(x) ... -
排序算法和二分查找
2009-05-17 15:50 790using namespace std; #includ ... -
C移位
2009-05-15 09:56 1346【转】 C提供了六种位运算运算符;这些运算符可能只允许整型操作 ... -
sizeof union struct 内存对齐
2009-05-14 20:30 2527【转】http://www.programfan.com/bl ... -
求100的阶乘
2009-05-06 16:32 1510#include <stdio.h> int m ... -
C字符串反转
2009-05-03 10:53 1761更改下面程序 #include string.h ... -
C链表相关
2009-05-03 10:38 772#include "stdafx.h" ... -
C题库连接
2009-04-29 09:28 747http://blog.chinaunix.net/u2/64 ...
相关推荐
易语言字符串操作源码,字符串操作,字符串_取长度,字符串_取中间,字符串_取左边,字符串_取右边,字符串_替换,到宽字符,到多字节,取文本数据地址,取字节集数据地址,MultiByteToWideChar,WideCharToMultiByte
C语言字符串操作大全C语言字符串操作大全C语言字符串操作大全
字符串操作类,与VC CString类接口差不多,线程安全,跨平台,已经在windows 下VC和linux下g++编译通过
js字符串操作大全,基本上字符串操作都可以在里面找到
javascript字符串操作,有对其中字符串的各种操作方法
c++字符串操作函数集合整理c++字符串操作函数集合整理
C#-字符串操作类(替换字符串中危险字符、指定位置替换字符串、指定长度缩减字段并加...、指定字符串分割字符串、指定字符串位置获取字符串、过滤SQL中非法字符、检查SQL语句中是否有非法关键字、随机字符串生成、...
C#语言编程中,字符串操作编程基本操作的总结,入门级别介绍
本js集合了大量常用的js字符串操作,1,小数点处理,2.空格处理,3.数据类型校验,4:过滤特殊字符;5:url参数获取等等
/// 字符串操作类 /// 1、GetStrArray(string str, char speater, bool toLower) 把字符串按照分隔符转换成 List /// 2、GetStrArray(string str) 把字符串转 按照, 分割 换为数据 /// 3、GetArrayStr(List list...
1.字符串操作演示(Visual C++编程 源代码)1.字符串操作演示(Visual C++编程 源代码)1.字符串操作演示(Visual C++编程 源代码)1.字符串操作演示(Visual C++编程 源代码)1.字符串操作演示(Visual C++编程 源...
c#字符串操作方法实例
C#中字符串操作函数
讲述字符串操作!字符串操作函数大全(String)
之所以抛弃char*的字符串而选用C++标准程序库中的string类,是因为他和前者比较起来,不必担心内存是否足够、字符串长度等等,而且作为一个类出现,他集成的操作函数足以完成我们大多数情况下(甚至是100%)的需要。...
收集一些很有用的字符串操作类C#,适合拿来主义者。
JavaScript中常见的字符串操作函数及用法,详细说明JS中字符串的操作函数
web项目中常用的字符串操作类,可以直接下载后,修改包名就可以了
pb 函数库之字符串操作函数 pb 函数库提供了多种字符串操作函数,用于完成不同的字符串处理任务。下面是对这些函数的详细介绍: 1. Fill() 函数 Fill() 函数用于建立一个由指定字符串填充的指定长度的字符串。该...