C语言中怎么将调用函数传递的字符转换为字符串?

bT_Td 2018-08-02 11:12:12
课本习题是:编写一个函数is_within()。他接收两个参数,一个是字符,另一个是字符串指针。其功能是如果字符在字符串中,就返回一个非0值(真);如果字符不在字符串中,就返回0值(假)。在一个使用循环语句为这个函数提供输入的完整程序中进行测试。

我想的是把字符传递到函数is_within()中后转换为字符串,再用函数strcmp()做比较,求dalao们指点一下。谢谢
...全文
599 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2018-08-02
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\intel\strchr.asm
		page	,132
title strchr - search string for given character
;***
;strchr.asm - search a string for a given character
;
; Copyright (c) Microsoft Corporation. All rights reserved.
;
;Purpose:
; defines strchr() - search a string for a character
;
;*******************************************************************************

.xlist
include cruntime.inc
.list

page
;***
;char *strchr(string, chr) - search a string for a character
;
;Purpose:
; Searches a string for a given character, which may be the
; null character '\0'.
;
; Algorithm:
; char *
; strchr (string, chr)
; char *string, chr;
; {
; while (*string && *string != chr)
; string++;
; if (*string == chr)
; return(string);
; return((char *)0);
; }
;
;Entry:
; char *string - string to search in
; char chr - character to search for
;
;Exit:
; returns pointer to the first occurence of c in string
; returns NULL if chr does not occur in string
;
;Uses:
;
;Exceptions:
;
;*******************************************************************************

CODESEG

found_bx:
lea eax,[edx - 1]
pop ebx ; restore ebx
ret ; _cdecl return

align 16
public strchr, __from_strstr_to_strchr
strchr proc \
string:ptr byte, \
chr:byte

OPTION PROLOGUE:NONE, EPILOGUE:NONE

.FPO ( 0, 2, 0, 0, 0, 0 )

xor eax,eax
mov al,[esp + 8] ; al = chr (search char)

__from_strstr_to_strchr label proc

push ebx ; PRESERVE EBX
mov ebx,eax ; ebx = 0/0/0/chr
shl eax,8 ; eax = 0/0/chr/0
mov edx,[esp + 8] ; edx = buffer
test edx,3 ; test if string is aligned on 32 bits
jz short main_loop_start

str_misaligned: ; simple byte loop until string is aligned
mov cl,[edx]
add edx,1
cmp cl,bl
je short found_bx
test cl,cl
jz short retnull_bx
test edx,3 ; now aligned ?
jne short str_misaligned

main_loop_start: ; set all 4 bytes of ebx to [chr]
or ebx,eax ; ebx = 0/0/chr/chr
push edi ; PRESERVE EDI
mov eax,ebx ; eax = 0/0/chr/chr
shl ebx,10h ; ebx = chr/chr/0/0
push esi ; PRESERVE ESI
or ebx,eax ; ebx = all 4 bytes = [chr]

; in the main loop (below), we are looking for chr or for EOS (end of string)

main_loop:
mov ecx,[edx] ; read dword (4 bytes)
mov edi,7efefeffh ; work with edi & ecx for looking for chr

mov eax,ecx ; eax = dword
mov esi,edi ; work with esi & eax for looking for EOS

xor ecx,ebx ; eax = dword xor chr/chr/chr/chr
add esi,eax

add edi,ecx
xor ecx,-1

xor eax,-1
xor ecx,edi

xor eax,esi
add edx,4

and ecx,81010100h ; test for chr
jnz short chr_is_found ; chr probably has been found

; chr was not found, check for EOS

and eax,81010100h ; is any flag set ??
jz short main_loop ; EOS was not found, go get another dword

and eax,01010100h ; is it in high byte?
jnz short retnull ; no, definitely found EOS, return failure

and esi,80000000h ; check was high byte 0 or 80h
jnz short main_loop ; it just was 80h in high byte, go get
; another dword
retnull:
pop esi
pop edi
retnull_bx:
pop ebx
xor eax,eax
ret ; _cdecl return

chr_is_found:
mov eax,[edx - 4] ; let's look one more time on this dword
cmp al,bl ; is chr in byte 0?
je short byte_0
test al,al ; test if low byte is 0
je retnull
cmp ah,bl ; is it byte 1
je short byte_1
test ah,ah ; found EOS ?
je retnull
shr eax,10h ; is it byte 2
cmp al,bl
je short byte_2
test al,al ; if in al some bits were set, bl!=bh
je retnull
cmp ah,bl
je short byte_3
test ah,ah
jz retnull
jmp short main_loop ; neither chr nor EOS found, go get
; another dword
byte_3:
pop esi
pop edi
lea eax,[edx - 1]
pop ebx ; restore ebx
ret ; _cdecl return

byte_2:
lea eax,[edx - 2]
pop esi
pop edi
pop ebx
ret ; _cdecl return

byte_1:
lea eax,[edx - 3]
pop esi
pop edi
pop ebx
ret ; _cdecl return

byte_0:
lea eax,[edx - 4]
pop esi ; restore esi
pop edi ; restore edi
pop ebx ; restore ebx
ret ; _cdecl return

strchr endp
end

赵4老师 2018-08-02
  • 打赏
  • 举报
回复
C:\Program Files\Microsoft Visual Studio 10.0\VC\crt\src\strchr.c
/***
*strchr.c - search a string for a given character
*
* Copyright (c) Microsoft Corporation. All rights reserved.
*
*Purpose:
* defines strchr() - search a string for a character
*
*******************************************************************************/

#include <cruntime.h>
#include <string.h>

/***
*char *strchr(string, c) - search a string for a character
*
*Purpose:
* Searches a string for a given character, which may be the
* null character '\0'.
*
*Entry:
* char *string - string to search in
* char c - character to search for
*
*Exit:
* returns pointer to the first occurence of c in string
* returns NULL if c does not occur in string
*
*Exceptions:
*
*******************************************************************************/

char * __cdecl strchr (
const char * string,
int ch
)
{
while (*string && *string != (char)ch)
string++;

if (*string == (char)ch)
return((char *)string);
return(NULL);
}
自信男孩 2018-08-02
  • 打赏
  • 举报
回复
引用 2 楼 bT_Td 的回复:
[quote=引用 1 楼 cfjtaishan 的回复:]
不建议这么做,若把字符变成字符串,比较简单。但是比较用strcmp反而增加了解决问题的复杂度。因为这个函数的功能就有点像strncmp,n就是1.

字符变成字符串方法

char ch = 'a';
char str[2];
str[0] = ch;
str[1] = 0;

str就是字符串

谢谢,那有没有什么好的思路呢,暂时就想到这个方法了[/quote]

遍历比较就好了


for (i = 0; src[i]; i++)
if (src[i] == key)
break;
if (src[i] == '\0')
return 0
return i;

逻辑没那么复杂,key是要查询的字符,src是字符串
bT_Td 2018-08-02
  • 打赏
  • 举报
回复
引用 1 楼 cfjtaishan 的回复:
不建议这么做,若把字符变成字符串,比较简单。但是比较用strcmp反而增加了解决问题的复杂度。因为这个函数的功能就有点像strncmp,n就是1.

字符变成字符串方法

char ch = 'a';
char str[2];
str[0] = ch;
str[1] = 0;

str就是字符串

谢谢,那有没有什么好的思路呢,暂时就想到这个方法了
自信男孩 2018-08-02
  • 打赏
  • 举报
回复
不建议这么做,若把字符变成字符串,比较简单。但是比较用strcmp反而增加了解决问题的复杂度。因为这个函数的功能就有点像strncmp,n就是1.

字符变成字符串方法

char ch = 'a';
char str[2];
str[0] = ch;
str[1] = 0;

str就是字符串
bT_Td 2018-08-02
  • 打赏
  • 举报
回复
谢谢各位大佬
杀意已决 2018-08-02
  • 打赏
  • 举报
回复
string有find,substr等函数直接调就可以了。 不用string的话一个一个比
本课程采用了漫画+动手实操+练习讲授Python编程技能。课程简介:第6章 容器类型数据6.1 序列6.1.1 序列的索引操作6.1.2 加与乘操作6.1.3 切片操作6.1.4 成员测试6.2 列表6.2.1 创建列表6.2.2 追加元素6.2.3 插入元素6.2.4 替换元素6.2.5 删除元素6.3 元组6.3.1 创建元组6.3.2 元组拆包6.4 集合6.4.1 创建集合6.4.2 修改集合6.5 字典6.5.1 创建字典6.5.2 修改字典6.5.3 访问字典视图6.6 动动手 —— 遍历字典6.7 练一练第7章 字符7.1 字符的表示方式7.1.1 普通字符7.1.2 原始字符7.1.3 长字符7.2 字符与数字的相互转换7.2.1 将字符转换为数字7.2.2 将数字转换字符7.3 格式化字符7.3.1 使用占位符7.3.2 格式化控制符7.4 操作字符7.4.1 字符查找7.4.2 字符替换7.4.3 字符分割7.5 动动手 —— 统计英文文章单词出现的频率7.6 练一练第8章 函数8.1 定义函数8.2 调用函数8.2.1 使用位置参数调用函数8.2.2 使用关键字参数调用函数8.3 参数的默认值8.4 可变参数8.4.1 基于元组的可变参数( *可变参数)8.4.2 基于字典的可变参数( **可变参数)8.5 函数变量的作用域8.6 函数类型8.6.1 理解函数类型8.6.2 过滤函数filter()8.6.3 映射函数map()8.7 lambda()函数8.8 动动手 —— 使用更多的lambda()函数8.9 练一练第9章 类与对象9.1 面向对象9.2 定义类9.3 创建对象9.4 类的成员9.4.1 实例变量9.4.2 构造方法9.4.3 实例方法9.4.4 类变量19.5 封装9.5.1 私有变量9.5.2 私有方法9.5.3 使用属性9.6 继承性9.6.1 Python的继承9.6.2 多继承9.6.3 重写方法9.7 多态性9.7.1 继承与多态9.7.2 鸭子类型测试与多态9.8 练一练第10章 异常处理9.8 练一练10.1 第一个异常 —— 除零异常10.2 捕获异常110.2.1 try-except语句10.2.2 多个except代码块10.2.3 多重异常捕获10.2.4 try-except语句嵌套10.3 使用finally代码块释放资源10.4 自定义异常类10.5 动动手 —— 手动引发异常10.6 练一练

69,368

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧