如何判断WCHAR字符串数组判断为空是否为空

C 库函数 - wctomb()
C 库函数 int wctomb(char *str, wchar_t wchar) 把宽字符 wchar 转换为它的多字节表示形式,并把它存储在 str 指向的字符数组的开头。
下面是 wctomb() 函数的声明。
int wctomb(char *str, wchar_t wchar)
str -- 一个指针,指向一个足以存储多字节字符的数组。
wchar -- 类型为 wchar_t 的宽字符。
如果 str 不为 NULL,wctomb() 函数返回写入字节数组中的字节数。如果 wchar 不能被表示为一个多字节序列,则会返回 -1。
如果 str 为 NULL,如果编码具有移位状态,则 wctomb() 函数返回非零,如果编码是无状态的,则返回零。
下面的实例演示了 wctomb() 函数的用法。
#include &stdio.h&
#include &stdlib.h&
int main()
wchar_t wc = L'a';
char *pmbnull = NULL;
char *pmb = (char *)malloc(sizeof( char ));
printf("要转换的宽字符:\n");
i = wctomb( pmb, wc );
printf("被转换的字符:%u\n", i);
printf("多字节字符:%.1s\n", pmb);
printf("当要转换的字符为 NULL 时尝试转换:\n");
i = wctomb( pmbnull, wc );
printf("被转换的字符:%u\n", i);
/* 不会输出任何值 */
printf("多字节字符:%.1s\n", pmbnull);
return(0);
让我们编译并运行上面的程序,这将产生以下结果:
要转换的宽字符:
被转换的字符:1
多字节字符:a
当要转换的字符为 NULL 时尝试转换:
被转换的字符:0
多字节字符:
反馈内容(*必填)
截图标记颜色
联系方式(邮箱)
联系邮箱:
投稿页面:
记住登录状态
重复输入密码用数组如何实现在一个字符串中找到它的子串_C++,C语言_ThinkSAAS
用数组如何实现在一个字符串中找到它的子串
用数组如何实现在一个字符串中找到它的子串
内容来源: 网络
用数组怎么实现在一个字符串中找到它的子串让下列代码输出two three four five
#include&iostream&#include&string.h&char GetSubstr(char sub[],char str[]);void main(){ char substr[20]; substr[20]=GetSubstr("two","one two three four five"); cout&&"找到的子串为:"&&substr&&}char GetSubstr(char sub[],char str[])
{ int i=0,j=0; for(j=0;;j++)
{ if(sub[i]==str[j]) //如果相同,判断sub[i+1]与str[j+1]相同,(1)相同的话再判断sub[i+2]与
代码不会写??? //str[j+2]是 否相同,(2)不同的话继续寻找与sub[i]相同的元素,直到找到第一
//完整的第一个字符串 }
我的想法是将第二个字符串的每一个字符先于第一个字符串的的第一个字符比较,找到相同的,然后再将第二个字符串的相同字符串后的字符串一个一个再与第一个字符串中的第二个字符相比较,以此类推直到第一个字符串结束,然后再输出相同字符以及以后的字符。------解决方案--------------------C/C++ code
char GetSubstr(char sub[],char str[]);
char substr[20];
substr[20]=GetSubstr("two","one two three four five");
------解决方案--------------------
这个,C和C++都有现成的函数的,C的是strstr,C++可用string的find参考下BC的strstr.c源码C/C++ code
/*-----------------------------------*
* filename - strstr.c
* function(s)
strstr - scans a string for the occurrence of a given string
wcsstr - scans a wide-character string for the occurrence of a
given wide-characterstring
*-----------------------------------*/
C/C++ Run Time Library - Version 11.0
Copyright (c)
by Borland Software Corporation
All Rights Reserved.
/* $Revision: 9.8.2.1 $
#include &string.h&
#include &tchar.h&
/*-----------------------------------*
strstr, wcsstr - scans a string for the occurrence of a
given string.
char *strstr(const char *str1, const char *str2);
wchar_t *strstr(const wchar_t *str1, const wchar_t *str2);
Prototype in
Description
strstr and wcsstr scan str1 for the first occurrence of the
substring str2.
Return value
strstr and wcsstr return a pointer to the element in str1
that contains str2 (points to str2 in str1). If str2 does
not occur in str1, strstr returns NULL.
*------------------------------------*/
/* This routine works better without any extra optimization at this point */
#pragma option push -Od -r
_TCHAR * _RTLENTRY _EXPFUNC _tcsstr(const _TCHAR *str1, const _TCHAR *str2)
_TCHAR *c1, *c2, *s = (_TCHAR *)str1;
if (!*str2)
return (_TCHAR *)str1;
/* return str1 if str2 is empty */
if (!*str1)
return NULL;
/* return NULL if str1 is empty */
if (*s == *str2)
/* wait for the first matching char */
c2 = (_TCHAR*)str2;
/* continue matching chars in the sub-str */
while (*c1 && *c2 && (!(*c2 - *c1)))
/* if we&ve run off the end of str2 */
/* then we&ve found the first substr match */
/* otherwise try the next char */
return NULL;
/* didn&t find anything */
#pragma option pop
PHP开发框架
开发工具/编程工具
服务器环境
ThinkSAAS商业授权:
ThinkSAAS为用户提供有偿个性定制开发服务
ThinkSAAS将为商业授权用户提供二次开发指导和技术支持
让ThinkSAAS更好,把建议拿来。
开发客服微信 上传我的文档
 下载
 收藏
该文档贡献者很忙,什么也没留下。
 下载此文档
正在努力加载中...
课后习题答案
下载积分:1000
内容提示:C
课后习题答案
文档格式:PDF|
浏览次数:15|
上传日期: 20:16:53|
文档星级:
全文阅读已结束,如果下载本文需要使用
 1000 积分
下载此文档
该用户还上传了这些文档
课后习题答案
官方公共微信2009年 总版技术专家分年内排行榜第四
2009年2月 总版技术专家分月排行榜第一2009年1月 总版技术专家分月排行榜第一2008年11月 总版技术专家分月排行榜第一2008年10月 总版技术专家分月排行榜第一2008年9月 总版技术专家分月排行榜第一2008年8月 总版技术专家分月排行榜第一2008年7月 总版技术专家分月排行榜第一2008年6月 总版技术专家分月排行榜第一2008年5月 总版技术专家分月排行榜第一2008年4月 总版技术专家分月排行榜第一
2009年1月 VC/MFC大版内专家分月排行榜第三
2013年4月 VC/MFC大版内专家分月排行榜第一2007年7月 VC/MFC大版内专家分月排行榜第一2007年5月 VC/MFC大版内专家分月排行榜第一2007年4月 VC/MFC大版内专家分月排行榜第一2007年3月 VC/MFC大版内专家分月排行榜第一
2013年3月 VC/MFC大版内专家分月排行榜第二2013年2月 VC/MFC大版内专家分月排行榜第二2008年8月 VC/MFC大版内专家分月排行榜第二2008年7月 VC/MFC大版内专家分月排行榜第二2007年9月 VC/MFC大版内专家分月排行榜第二2007年8月 VC/MFC大版内专家分月排行榜第二2005年12月 VC/MFC大版内专家分月排行榜第二2005年10月 VC/MFC大版内专家分月排行榜第二
本帖子已过去太久远了,不再提供回复功能。

我要回帖

更多关于 判断字符串在数组中 的文章

 

随机推荐