简体   繁体   English

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

[英]Input C-style string and get the length

The string input format is like this 字符串输入格式是这样的

str1 str2 str1 str2

I DONT know the no. 我不知道不。 of characters to be inputted beforehand so need to store 2 strings and get their length. 预先输入的字符数,因此需要存储2个字符串并获取其长度。 Using the C-style strings ,tried to made use of the scanf library function but was actually unsuccessful in getting the length.This is what I have: 使用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;


   }

Not efficient mainly because of the memset calls. 效率不高主要是因为memset调用。

Note: I'd be better off using std::string but then because of 25000 length input and memory constraints of cin I switched to this.If there is an efficient way to get a string then it'd be good 注意:我最好使用std::string但是由于25000的长度输入和cin的内存限制,我切换到了这一点。如果有一种有效的方式来获取字符串,那会很好

Aside from the answers already given, I think your code is slightly wrong: 除了已经给出的答案之外,我认为您的代码略有错误:

   memset(M,'0',25000);memset(W,'0',25000);

Do you really mean to fill the string with the character zero (value 48 or 0x30 [assuming ASCII before some pedant downvotes my answer and points out that there are other encodings]), or with a NUL (character of the value zero). 您是否真的要用字符零填充字符串(值48或0x30 [假设某些脚踏子之前使用ASCII否决我的答案并指出存在其他编码])或使用NUL(字符零值)填充字符串。 The latter is 0 , not '0' 后者是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'
          }

If you are looking for the end of the string, you should be using 0 , not '0' (as per above). 如果要查找字符串的末尾,则应使用0而不是'0' (如上所述)。

Of course, scanf will put a 0 a the end of the string for you, so there's no need to fill the whole string with 0 [or '0' ]. 当然, scanf会为您在字符串的末尾放置0 a,因此不需要用0 [或'0' ]填充整个字符串。

And strlen is an existing function that will give the length of a C style string, and will most likely have a more clever algorithm than just checking each character and increment two variables, making it faster [for long strings at least]. 并且strlen是一个现有函数,将提供C样式字符串的长度,并且比起仅检查每个字符并增加两个变量,它更有可能具有更聪明的算法,从而使其速度更快(至少对于长字符串而言)。

You do not need memset when using scanf , scanf adds the terminating '\\0' to string. 使用scanf时不需要memset ,scanf将终止符'\\0'到字符串中。

Also, strlen is more simple way to determine string's length: 同样, 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);

C-style strlen without memset may looks like this: 没有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;
}

It's return 14. 返回14。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM