在C中解析字符串

发布于 2024-07-22 14:34:58 字数 132 浏览 7 评论 0原文

仅使用 C

我想解析一个字符串并:

  1. 计算字符串中字符的出现次数(例如,计算传入字符串中的所有 'e'
  2. 一次计数(甚至当我数数时)将 e 替换为 3

Using just C

I would like to parse a string and:

  1. count the occurrences of a character in a string (for example, count all the 'e's in a passed in string)
  2. Once counted (or even as I am counting) replace the e's with 3's

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

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

发布评论

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

评论(5

来世叙缘 2024-07-29 14:34:58

好吧,你要么懒惰,要么卡住了,假设卡住了。

您需要一个具有类似签名的函数

int ReplaceCharInString(char* string, char charToFind, char charThatReplaces)
{

}

在函数内部您需要

  1. 声明一个整数来计数
    出现
  2. 次数 从开头移动的循环
    字符串到
  3. 循环内的末尾,if 语句到
    检查当前字符
    charToFind,
  4. 增加计数的语句
    发生并执行
    replacement
  5. 循环结束后,需要返回
    出现次数

OK, you're either lazy, or stuck, assuming stuck.

You need a function with a signature something like

int ReplaceCharInString(char* string, char charToFind, char charThatReplaces)
{

}

Inside the function you need

  1. To declare an integer to count the
    occurrences
  2. A loop that moves from the start of
    the string to it's end
  3. inside the loop, an if statement to
    check is the current char the
    charToFind,
  4. statements to increment the count of
    occurrences and perform the
    replacement
  5. After the loop, you need to return
    the count of occurrences
谁人与我共长歌 2024-07-29 14:34:58

该函数将采用一个字符串,将每个“e”替换为“3”,并返回执行替换的次数。 它安全、干净、快捷。

int e_to_three(char *s)
{
    char *p;
    int count = 0;
    for (p = s; *p; ++p) {
        if (*p == 'e') {
            *p = '3';
            count++;
        }
    }
    return count;
}

This function will take a string, replace every 'e' with '3', and return the number of times it performed the substitution. It's safe, it's clean, it's fast.

int e_to_three(char *s)
{
    char *p;
    int count = 0;
    for (p = s; *p; ++p) {
        if (*p == 'e') {
            *p = '3';
            count++;
        }
    }
    return count;
}
萌化 2024-07-29 14:34:58

这是一个帮助您入门的 shell。 如果您需要任何帮助,请在此询问。

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

int main(){
    const char* string = "hello world";
    char buffer[256];
    int e_count = 0;
    char* walker;

    // Copy the string into a workable buffer
    strcpy(buffer,string);

    // Do the operations
    for(walker=buffer;*walker;++walker){
        // Use *walker to read and write the current character
    }

    // Print it out
    printf("String was %s\nNew string is %s\nThere were %d e's\n",string,buffer,e_count);
}

Here's a shell to get you started. Ask here if you need any help.

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

int main(){
    const char* string = "hello world";
    char buffer[256];
    int e_count = 0;
    char* walker;

    // Copy the string into a workable buffer
    strcpy(buffer,string);

    // Do the operations
    for(walker=buffer;*walker;++walker){
        // Use *walker to read and write the current character
    }

    // Print it out
    printf("String was %s\nNew string is %s\nThere were %d e's\n",string,buffer,e_count);
}
り繁华旳梦境 2024-07-29 14:34:58

一般来说,最好使用标准库函数而不是自己编写。 而且,正如它所发生的那样,有一个标准库函数可以在字符串中搜索字符并返回指向该字符的指针。 (它处理一个字符串,因此请查看具有前缀“str”的函数)(库函数几乎肯定会被优化以使用专门的 CPU 操作码来完成任务,而手写代码则不会)

  1. 设置指向字符串开头的临时指针(例如“ptr”)。

    • 在循环中,使用 ptr 作为参数调用上面的函数,并将其设置为返回值。

    • 增加计数器。

    • 当未找到“e”时,将指针处的字符设置为“3”。

In general, it's better use a standard library function rather than rolling your own. And, as it just so happens, there is a standard library function that searches a string for a character and returns a pointer to it. (It deals with a string, so look among the functions that have the prefix "str") (The library function will almost certainly be optimized to use specialized CPU opcodes for the task, that hand written code would not)

  1. Set a temp pointer (say "ptr") to the start of the string.

    • In a loop, call the function above using ptr as the parameter, and setting it to the return value.

    • Increment a counter.

    • Set the character at the pointer to "3" break when 'e' is not found.

惯饮孤独 2024-07-29 14:34:58

你们中的一些人是从中间开始的。

更好的开始是

char *string = "hello world";
Assert(ReplaceCharInString(string, 'e', '3') == 1);
Assert(strcmp(string, "h3llo world") == 0);

Some of you guys are starting in the middle.

A better start would be

char *string = "hello world";
Assert(ReplaceCharInString(string, 'e', '3') == 1);
Assert(strcmp(string, "h3llo world") == 0);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文