gpt4 book ai didi

c - C 编程中的子串

转载 作者:太空宇宙 更新时间:2023-11-04 02:41:37 25 4
gpt4 key购买 nike

我只想从字符串中提取特定的单词。
我的程序是:

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

#define BUFFER_SIZE 100

int main() {

FILE *f;
char buffer[100];
char buf[100];
int count=0;
char res[100];


f=fopen("1JAC.pdb","rb");

while(fgets(buffer,BUFFER_SIZE,f))
{

if(strncmp(buffer,"ATOM",4)==0 && strncmp(buffer+13,"CA",2)==0 && strncmp(buffer+21,"A",1)==0)
{
strcpy(buf,buffer);

}

printf (buf);

程序的输出是原子 1033 CA LEU A 133 33.480 94.428 72.166 1.00 16.93 C

我只想使用子字符串提取单词“LEU”。我试过这样的事情:

Substring(17,3,buf);

但是没用...
有人能告诉我 C 中的子串吗?

最佳答案

Memcpy 似乎是执行此操作的最佳方式......

memcpy( destBuff, sourceBuff + 17, 3 );
destBuff[ 3 ] = '\0';

请记得在需要时添加空终止符(正如我在示例中所做的那样)。

之前也已经在 Stack-overflow 上多次回答过这个问题

( Get a substring of a char* )

关于c - C 编程中的子串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31314847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com