C++ 输入C样式字符串并获取长度

C++ 输入C样式字符串并获取长度,c++,char,C++,Char,字符串输入格式如下 str1和str2 我不知道预先输入的字符数,所以需要存储2个字符串并获取其长度。 使用C样式字符串,尝试使用scanf库函数,但实际上无法获得长度。我的情况如下: // M W are arrays of char with size 25000 while (T--) { memset(M,'0',25000);memset(W,'0',25000); scanf("%s",M); scanf("%s",W);

字符串输入格式如下

str1和str2

我不知道预先输入的字符数,所以需要存储2个字符串并获取其长度。 使用C样式字符串,尝试使用scanf库函数,但实际上无法获得长度。我的情况如下:

// M W are arrays of char with size 25000
   while (T--)
   {
       memset(M,'0',25000);memset(W,'0',25000);
       scanf("%s",M);
       scanf("%s",W);
       i = 0;m = 0;w = 0;
       while (M[i] != '0')
              {
                  ++m; ++i;  // incrementing till array reaches '0'
              }
        i = 0;
       while (W[i] != '0')
              {
                  ++w; ++i;
              }
       cout << m << w;


   }
//mw是大小为25000的字符数组
而(T--)
{
记忆集(M,'0',25000);记忆集(W,'0',25000);
scanf(“%s”,M);
scanf(“%s”,W);
i=0;m=0;w=0;
而(M[i]!=“0”)
{
++m、 ++i;//递增直到数组达到“0”
}
i=0;
而(W[i]!=“0”)
{
++w、 ++i;
}

当使用
scanf
时,您不需要
memset
,scanf将终止的
'\0'
添加到字符串中

另外,
strlen
是确定字符串长度的更简单的方法:

scanf("%s %s", M, W); // provided that M and W contain enough space to store the string
m = strlen(M); // don't forget #include <string.h>
w = strlen(W);
scanf(“%s%s”,M,W);//前提是M和W包含足够的空间来存储字符串
m=strlen(m);//别忘了#包括
w=斯特伦(w);

使用
scanf
时,不需要
memset
,scanf会将终止的
'\0'
添加到字符串中

另外,
strlen
是确定字符串长度的更简单的方法:

scanf("%s %s", M, W); // provided that M and W contain enough space to store the string
m = strlen(M); // don't forget #include <string.h>
w = strlen(W);
scanf(“%s%s”,M,W);//前提是M和W包含足够的空间来存储字符串
m=strlen(m);//别忘了#包括
w=斯特伦(w);

除了已经给出的答案,我认为您的代码有点错误:

   memset(M,'0',25000);memset(W,'0',25000);
你真的想用字符0(值48或0x30[假设在一些学究否决我的答案并指出还有其他编码之前使用ASCII])或NUL(值为零的字符)填充字符串吗?后者是
0
,而不是
'0'

   scanf("%s",M);
   scanf("%s",W);
   i = 0;m = 0;w = 0;
   while (M[i] != '0')
          {
              ++m; ++i;  // incrementing till array reaches '0'
          }
如果要查找字符串的结尾,应使用
0
,而不是
'0'
(如上所述)

当然,
scanf
会为您在字符串末尾添加一个
0
a,因此不需要用
0
[或
'0'
]填充整个字符串


strlen是一个现有的函数,它可以给出C风格字符串的长度,并且很可能有一个比只检查每个字符并增加两个变量更聪明的算法,使它更快[至少对于长字符串]。

除了已经给出的答案之外,我认为您的代码有点错误:

   memset(M,'0',25000);memset(W,'0',25000);
你真的想用字符0(值48或0x30[假设在一些学究否决我的答案并指出还有其他编码之前使用ASCII])或NUL(值为零的字符)填充字符串吗?后者是
0
,而不是
'0'

   scanf("%s",M);
   scanf("%s",W);
   i = 0;m = 0;w = 0;
   while (M[i] != '0')
          {
              ++m; ++i;  // incrementing till array reaches '0'
          }
如果要查找字符串的结尾,应使用
0
,而不是
'0'
(如上所述)

当然,
scanf
会为您在字符串末尾添加一个
0
a,因此不需要用
0
[或
'0'
]填充整个字符串


strlen
是一个现有的函数,它将给出C样式字符串的长度,并且很可能会有一个比只检查每个字符并增加两个变量更聪明的算法,使其更快[至少对于长字符串]。

没有memset的C样式strlen可能如下所示:

#include <iostream>
using namespace std;

unsigned strlen(const char *str) {
    const char *p = str;
    unsigned len = 0;
    while (*p != '\0') {
        len++;
        *p++;
    }
    return len;
}

int main() {
    cout << strlen("C-style string");
    return 0;
}
#包括
使用名称空间std;
无符号strlen(常量字符*str){
const char*p=str;
无符号len=0;
而(*p!='\0'){
len++;
*p++;
}
回程透镜;
}
int main(){

coutC-风格的strlen没有memset可能看起来像这样:

#include <iostream>
using namespace std;

unsigned strlen(const char *str) {
    const char *p = str;
    unsigned len = 0;
    while (*p != '\0') {
        len++;
        *p++;
    }
    return len;
}

int main() {
    cout << strlen("C-style string");
    return 0;
}
#包括
使用名称空间std;
无符号strlen(常量字符*str){
const char*p=str;
无符号len=0;
而(*p!='\0'){
len++;
*p++;
}
回程透镜;
}
int main(){

因为这是C++,所以你应该使用<代码> STD:String 和C++ I/O——如果你真的需要它,你可以从一个代码> STD::String 中得到一个C字符串。因为这是C++,你应该使用<代码> STD:String 和C++ I/O——如果你真的需要它,你可以从一个<代码> STD::String 中获得一个C字符串。