使用4种方法来算字符串长度
2009-04-02 05:16
288 查看
str::strlen,
string::length,
loop操作,
形式语言的标准递归。
string::length,
loop操作,
形式语言的标准递归。
#include <cstdio> #include <string> #include <cstring> #include <iostream> using namespace std; int strlen_by_loop( char* const psz ) { char* p = psz; int iLen = 0; while( *p++ ) iLen++; return iLen; }// int strlen_by_recusive( char* const psz ) { if( *psz == '/0' ) return 0; else return strlen_by_recusive( psz + 1 ) + 1; }// int main() { char* psz = "hello,world"; string str( psz ); cout<<"Length of 'hello,world' by 'std::strlen()' = "<<strlen( psz )<<endl <<"Length of 'hello,world' by 'string::length()' = "<<str.length()<<endl <<"Length of 'hello,world' by 'strlen_by_loop' = "<<strlen_by_loop( psz )<<endl <<"Length of 'hello,world' by 'strlen_by_recusive' = "<<strlen_by_recusive( psz )<<endl<<endl; return 0; }//
相关文章推荐
- C++不使用变量求字符串长度strlen函数的实现方法
- 测试了3种获取中英文混合字符串长度的方法,看来还是使用循环来的快
- 4种使用javascript正则表达式进行字符串replace操作的方法
- 关于对字符串、数组处理的常见问题与方法,如获取带有空格字符串长度,数组长度等。getline(),sprintf()使用。
- 不使用strlen函数求字符串的长度的方法
- vim使用方法----拷贝和删除任意长度字符串
- 用Java实现按字节长度截取字符串的方法
- 在线聊天项目1.4版 使用Gson方法解析Json字符串以便重构request和response的各种请求和响应 解决聊天不畅问题 Gson包下载地址
- 一步步 jQuery (一)概念,使用,$名称冲突4种解决方法,使用层次及次数问题
- 在Java中按字节获得字符串长度的两种方法
- 中英文混合字符串判定长度方法
- 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错。字符串的长度超过了为 maxJsonLength 属性设置的值
- 使用js在HTML中自定义字符串格式化方法
- c#打包文件解压缩 C#中使用委托、接口、匿名方法、泛型委托实现加减乘除算法 一个简单例子理解C#的协变和逆变 对于过长字符串的大小比对
- 处理字符串的一些js/jq方法(去除HTML,去除空格,计算真实长度,截取中英文字符)
- php将字符串随机分割成不同长度数组的方法
- 读取 XML 数据时,超出最大字符串内容长度配额 (8192)。通过更改在创建 XML 读取器时所使用的 XmlDictionaryReaderQuotas 对象的 MaxStringContentLength 属性,可增加此配额。【轉】
- JS截取字符串常用方法整理及使用示例
- php将字符串随机分割成不同长度数组的方法
- string 字符串使用 - 字符处理方法