在按字符串按' - &#x27中的字符串中找到每个字符的ASCII。

发布于 2025-01-17 18:59:21 字数 303 浏览 3 评论 0原文

所以,我想用c编写一个程序,在其中输入一个字符串,然后输出将是该字符串中每个字符的ascii编号,并用“-”分隔。我几乎完成了,然后我不知道如何删除代码输出中的最后一个“-”。实验值。输入:abc 输出:97-98-99。 这是我的代码:

    char s[1001];
    
    scanf("%s",&s);
    for(int i=0;s[i]!='\0';i++){
        printf("%d-",s[i]);
    }
    return 0;
}

我的输出:97-98-99-

So, i want to make a program in c where you input a string then the output will be ascii number of every character in that string split by '-'. i almost done then i dont know how to remove the last '-' in the output of my code. exp. input: abc output: 97-98-99.
Here is my code :

    char s[1001];
    
    scanf("%s",&s);
    for(int i=0;s[i]!='\0';i++){
        printf("%d-",s[i]);
    }
    return 0;
}

My output : 97-98-99-

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

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

发布评论

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

评论(4

葮薆情 2025-01-24 18:59:22

检查当前索引是否不是最后一个索引,然后打印-,否则它是最后一个索引并打印换行

char s[1001];

scanf("%s",&s);

int len = 0;

for(;s[len]!='\0';len++);

for(int i=0;i<len;i++){
    printf("%d",s[i]);
    if(i+1 != len)
       printf("-")
    else
        printf("\n")
}

check if current index is not the last index then print - otherwise it's the last index and print newline

char s[1001];

scanf("%s",&s);

int len = 0;

for(;s[len]!='\0';len++);

for(int i=0;i<len;i++){
    printf("%d",s[i]);
    if(i+1 != len)
       printf("-")
    else
        printf("\n")
}
那小子欠揍 2025-01-24 18:59:22

无需在循环中使用 strlen()length 或重复测试,只需更改分隔符即可。

const char *separator = "";
for (size_t i=0; s[i]!='\0'; i++) {
    printf("%s%d",separator, s[i]);
    separator = "-";
}

Instead of using strlen(), length or repeated tests in a loop, simply change the separator.

const char *separator = "";
for (size_t i=0; s[i]!='\0'; i++) {
    printf("%s%d",separator, s[i]);
    separator = "-";
}
落花随流水 2025-01-24 18:59:21

我们将使用三元运算符来检查i<通过检查s[i + 1] != 0,/code>是'\0'之前的最后一个字符,这里i是当前字符串的索引。这是一个视觉表示

注意:您的变量 char s[1001] 将衰减为指针(例外:sizeof 运算符)。

一些改进:

  • 不要使用"%s",使用"%s",以避免缓冲区溢出
  • 使用 size_t 迭代数组。
  • 不要使用裸露的 return 0;,而是使用 return EXIT_SUCCESS;,它是在头文件 stdlib.h 中定义的。
  • 始终检查 scanf() 输入是否成功

最终代码:

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

int main(void)
{
    char s[1001] = {0};
    if (scanf("%1000s", s) != 1)
    {
        perror("bad input");
        return EXIT_FAILURE;
    }
    for (size_t i = 0; s[i]; i++)
        printf("%d%s", s[i], (s[i + 1]) ? "-" : "");
    puts("\n");
    return EXIT_SUCCESS;
}

输出:

Hello
72-101-108-108-111

We'll use Ternary Operator, to check whether i is the last character before '\0' by checking s[i + 1] != 0, here i is the current index of the string. Here's a visual representation.

NOTE: Your variable char s[1001] will be decayed to a pointer (exception: sizeof operator).

Some improvements:

  • Don't use "%s", use "%<WIDTH>s", to avoid buffer-overflow
  • Use size_t to iterate through an array.
  • Instead of using bare return 0;, use return EXIT_SUCCESS;, which is defined in the header file stdlib.h.
  • always check whether scanf() input was successful or not

Final Code:

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

int main(void)
{
    char s[1001] = {0};
    if (scanf("%1000s", s) != 1)
    {
        perror("bad input");
        return EXIT_FAILURE;
    }
    for (size_t i = 0; s[i]; i++)
        printf("%d%s", s[i], (s[i + 1]) ? "-" : "");
    puts("\n");
    return EXIT_SUCCESS;
}

Output:

Hello
72-101-108-108-111
原野 2025-01-24 18:59:21

稍微重新排列您的逻辑:

for(int i=0;s[i]!='\0';i++){
  if( i != 0 )
    printf("-");
  printf("%d",s[i]);
}

Slightly rearrange your logic:

for(int i=0;s[i]!='\0';i++){
  if( i != 0 )
    printf("-");
  printf("%d",s[i]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文