将字符串和 int 与 c++ 中的输入字符串分开

发布于 2024-12-07 09:38:44 字数 467 浏览 0 评论 0原文

我正在尝试对输入字符串中的整数和字符串进行排序。

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char x[10];
    int y;
    printf("string: ");
    scanf("%s",x);
    y=atoi(x);
    printf("\n %d", y);
    getchar();
    getchar(); }

假设输入是123abc1 使用atoi我可以从输入字符串中提取123,我现在的问题是如何提取abc1?

我想将 abc1 存储在单独的字符变量上。

输入:123abc1 输出:x = 123,一些 char 变量 = abc1

我感谢任何帮助。

I am trying to sort integers and strings from an input string.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>

int main(){
    char x[10];
    int y;
    printf("string: ");
    scanf("%s",x);
    y=atoi(x);
    printf("\n %d", y);
    getchar();
    getchar(); }

suppose the input is 123abc1
using atoi i could extract 123 from the input string, my question now is how do i extract abc1?

I want to store abc1 on a separate character variable.

input: 123abc1
output: x = 123, some char variable = abc1

I appreciate any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅唱ヾ落雨殇 2024-12-14 09:38:44

如果您希望使用 C 编程语言概念,请考虑使用 strtol 而不是 atoi。它会让您知道它停在哪个字符处:

此外,切勿在 scanf 中使用 %s,始终指定缓冲区大小(减一,因为 %s 会添加存储输入后的 '\0')

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    printf("string: ");
    char x[10];
    scanf("%9s",x);
    char *s;
    int y = strtol(x, &s, 10);
    printf("String parsed as:\ninteger: %d\nremainder of the string: %s\n",y, s);
}

测试: https://ideone.com/uCop8

在 C++ 中,如果该标签没有错误,还有更简单的方法,例如流 I/O。

例如,

#include <iostream>
#include <string>
int main()
{
    std::cout << "string: ";
    int x;
    std::string s;
    std::cin >> x >> s;
    std::cout << "String parsed as:\ninteger: " << x << '\n'
              << "remainder of the string: " << s << '\n';
}

测试:https://ideone.com/dWYPx

If you wish to use the C programming language concepts, then consider using strtol intead of atoi. It will let you know what character did it stop at:

Also, never use %s in a scanf, always specify the buffer size (minus one, since %s will add a '\0' after storing your input)

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    printf("string: ");
    char x[10];
    scanf("%9s",x);
    char *s;
    int y = strtol(x, &s, 10);
    printf("String parsed as:\ninteger: %d\nremainder of the string: %s\n",y, s);
}

test: https://ideone.com/uCop8

In C++, if that tag was not a mistake, there are simpler approaches, such as stream I/O.

For example,

#include <iostream>
#include <string>
int main()
{
    std::cout << "string: ";
    int x;
    std::string s;
    std::cin >> x >> s;
    std::cout << "String parsed as:\ninteger: " << x << '\n'
              << "remainder of the string: " << s << '\n';
}

test: https://ideone.com/dWYPx

陪你到最终 2024-12-14 09:38:44

如果这是您想要的方式,那么在提取数字后将其转换回其文本表示形式,并且该字符串长度将告诉您要找到字符串的开头。因此,对于您的特定示例:

char* x = "123abc1"
atoi( x ) -> 123;
itoa/sprintf( 123 ) -> "123", length 3
x + 3 -> "abc1"

您不能只用一个 scanf 来完成它吗?

scanf( "%d%s", &y, z );

If that's the way you want to go, then after extracting the number convert it back to its textual representation and that string length will tell you were to find the start of the string. So for your particular example:

char* x = "123abc1"
atoi( x ) -> 123;
itoa/sprintf( 123 ) -> "123", length 3
x + 3 -> "abc1"

Can't you just do it with a single scanf?

scanf( "%d%s", &y, z );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文