C 如果字符不等于
我怎么能说,C 如果字符不等于,c,character-encoding,C,Character Encoding,我怎么能说,ch不等于一个数字。。然后再回来。我也在尝试摆脱无限循环。试试这个: void encode(char* dst, const char* src) { while (1) { char ch = *src; if (!ch || ch != '0','1','2','3') { //pseudo-code *dst = 0; return; } size_t count = 1;
ch
不等于一个数字。。然后再回来。我也在尝试摆脱无限循环。试试这个:
void encode(char* dst, const char* src) {
while (1) {
char ch = *src;
if (!ch || ch != '0','1','2','3') { //pseudo-code
*dst = 0;
return;
}
size_t count = 1;
while (*(++src) == ch)
++count;
*(dst++) = ch;
dst += sprintf(dst, "%zu", count);
}
#包括
无效编码(字符*dst,常量字符*src){
而(1){
char ch=*src;
如果(!isdigit(ch)){//应该工作
*dst=0;
回来
}
//方法的其余部分
}
isdigit(int)
返回true
如果字符是整数(在另一种情况下为'1'、'2'、…、'9')或false
(我假设无限循环是故意完成的)这是我为您编写的一个小代码,它将解析所有
src
字符串,并告诉您该字符串是否为数字。while(1)可能存在的问题
是无限循环,您只查找第一个字符,因此如果我将2toto
作为参数传递,它会说ok,这是一个数字
如果src是一个数字,则此函数返回true;如果不是,则返回false
#include <ctype.h>
void encode(char* dst, const char* src) {
while (1) {
char ch = *src;
if (!isdigit(ch)) { //Should works
*dst = 0;
return;
}
// Rest of the method
}
#包括
布尔编码(字符*dst,常量字符*src){
int i=0;
char ch;
while(src[i]!='\0')
{
ch=src[i];
if(!isdigit(ch)){//伪码
*dst=0;
返回false;
}
i++;
}
返回true;
}
isdigit是有效的,但为了完成起见,您可以这样开始编写自己的字符范围检查:
#include <stdbool.h>
bool encode(char* dst, const char* src) {
int i = 0;
char ch;
while (src[i] != '\0')
{
ch = src[i];
if (!isdigit(ch)) { //pseudo-code
*dst = 0;
return false;
}
i++;
}
return true;
}
void编码(char*dst,const char*src){
而(1){
char ch=*src;
//字符数值减去
//字符“0”的数值
//应大于或等于
//如果字符是数字,则为零,
//它也应该小于或等于
//等于9
如果(!ch | |)(ch-'0'>=0&&ch-'0'你知道isdigit()
函数吗?另外,使用sscanf(“%[0-9]”)有什么不对
完美-谢谢你!'42'无效啊,你是对的,对不起:)!ch
是冗余的,isdigit
知道that@sgerbhctim你能证明答案的有效性吗?哦,勒博代码:o@YaatSukabah tu sais la piscine ca forge un home hein..xd这是一个以null结尾的字符@sgerbhctim,它在字符串上表示结束。另外,我该如何将其与上面的编辑集成
void encode(char* dst, const char* src) {
while (1) {
char ch = *src;
// the char numeric value minus the
// numeric value of the character '0'
// should be greater than or equal to
// zero if the char is a digit,
// and it should also be less than or
// equal to 9
if (!ch || (ch - '0' >= 0 && ch - '0' <= 9)) {
*dst = 0;
return;
}
// ...
}
}