c - 在 C 编程中使用字符串和子字符串的基础知识

标签 c string substring

我一直在尝试通过阅读教科书来学习 C 编程,但对字符串和子字符串的工作原理感到困惑。 我知道 Java 中的字符串和子字符串是什么,但无法弄清楚 C 中的语法。

这是书中的一个问题,我认为可能很简单,但我无法理解。

编写并测试一个氢氧化物函数,如果其字符串参数以子字符串 OH 结尾,则该函数返回 1,表示 true。

建议使用 KOH 和 NaCl 测试该功能。

另外,如何删除和添加字符串末尾的字母? 例如,如果出于某种原因我想将 NaCl 更改为 NaOH?

任何帮助和解释将不胜感激。

预计到达时间: 我想我最困惑的是如何让程序查看字符串中的最后两个字母并将它们与 OH 进行比较。 我也不确定如何将字符串传递给函数。

最佳答案

String 是一个以特殊 null 结尾的字符 '\0' 结尾的字符序列。如果没有 \0,则处理字符串的函数将不会停止,直到找到 \0 符号。该字符可能出现在字符串(我的意思是没有\0的字符串)末尾之后的任何位置,然后才停止。

以下示例显示了此空终止字符的必要性:

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

int main()
{
    char string[] = "Hello!";

    printf("original string:\n%s\n\n", string);

    memset(string, '-', 5);
    printf("memset doesn't affect the last two symbols: '!' and '\\0':\n%s", string);

    memset(string, '-', 6);
    printf("\n\nmemset doesn't affect the last symbol: '\\0':\n%s\n\n", string);

    memset(string, '-', 7);
    printf("memset affects all symbols including null-terminated one:\n%s", string);

    return 0;
}

/* OUTPUT:
original string:
Hello!

memset doesn't affect the last two characters: '!' and '\0':
-----!

memset doesn't affect the last character: '\0':
------

memset affects all characters including null-terminated one:
-------@↓@
*/
<小时/>

子字符串是字符串中的字符序列。它可能小于或等于字符串。 假设“NaOH”是一个字符串。那么子字符串可能是:"N""a""O""H"“Na”“aO”“OH”“NaO”“aOH”“NaOH”。要查找子字符串是否在字符串中,可以使用 strstr 函数。它的原型(prototype)是char * strstr (char * str1, const char * str2);

此代码显示此函数的结果:

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

int main()
{
    char *ptrCh = NULL;

    ptrCh = strstr("hello", "h");
    printf("ptrCh: %p\n", ptrCh);
    printf("%s\n\n", ptrCh);

    ptrCh = strstr("hello", "z");
    printf("ptrCh: %p\n", ptrCh);
    printf("%s\n\n", ptrCh);

    return 0;
}

/* OUTPUT:
ptrCh: 00403024
hello     

ptrCh: 00000000
(null)
*/

对于第一个 printf,它从“h”位置开始打印字符,当到达“o”之后的下一个空终止字符时,它会停止,与之前的程序完全相同。

<小时/>

为了使您的程序更具交互性,您可以声明数组,然后声明一个指向它的指针。数组大小必须足以存储最长的公式。假设 100 就足够了:

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

int main()
{
    char buf[100] = {0};
    char *ptr = &buf[0];

    scanf("%s", ptr);

    // printf() gets a pointer as argument
    printf("%s\n", ptr);   

    // printf() gets also a pointer as argument. 
    // When you pass arrays name without index to a function,
    // you pass a pointer to array's first element.
    printf("%s", buf);     

    return 0;
}
<小时/>

以及在字符串末尾重写字母。这是一个执行此操作的小程序。关注评论:

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

int main()
{
    char buf[100] = {0};
    char formula[100] = {0};
    char compound[100] = {0};
    char *ptr = &buf[0];
    char *pFormula = &formula[0];
    char *pCompound = &compound[0];

    printf("Enter formula: ");
    scanf("%s", pFormula);
    printf("Enter chemical compound: ");
    scanf("%s", pCompound);

    // Copying the first chemical elements without the last
    // several that will be replaced by another elements.
    strncpy(ptr, pFormula, strlen(pFormula) - strlen(pCompound));
    // Adding new compound to the first elements.
    // Function also adds a null-terminated character to the end.
    strncat(ptr, pCompound, strlen(pCompound));

    printf("The new chemical compound is: ");
    printf("%s", ptr);

    return 0;
}

/* OUTPUT:
Enter formula: NaOH
Enter chemical compound: Cl
The new chemical compound is: NaCl
*/

关于c - 在 C 编程中使用字符串和子字符串的基础知识,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26497949/

相关文章:

python - 在 Python 中,如何检查文件是否存在以子字符串开头或结尾?

python - 在/和\之间查找子字符串

c - CodeBlocks 中的奇怪警告 - C

在 Go 中更改 WebKitWebSettings cgo

c - (新手) strstr() 返回带有无符号参数的 null

在 R 中的一个 gsub() 或 chartr() 语句中替换多个字符串?

c++ - 放入 map 时 std::string 的范围

c++ - Hi-Link HLK-RM04跑openwrt,可以多线程吗?

c - 线程通过 sysfs 调用内核信号量的死锁

java - 如何获取同一索引的多个子字符串?