简体   繁体   English

修改字符串指针中的一部分字符串

[英]modify part of a string in a string pointer

I have a character pointer, which has a string assigned to it. 我有一个字符指针,为它分配了一个字符串。 And I'm sure that the string is of 8 characters length. 而且我确定该字符串的长度为8个字符。 (Ex: SHIVA0BS) And it is also a fact that the last two letters are always going to be "BS". (例如:SHIVA0BS)而且,最后两个字母始终都是“ BS”也是一个事实。 But I'm just going to double check it. 但我要仔细检查一下。 Now I'd like to take the first 6 characters ("SHIVA0") and append it to something else, say ("SHIVA0NN") - how would I make it possible? 现在,我想将前6个字符(“ SHIVA0”)附加到其他内容上,例如(“ SHIVA0NN”)-我将如何使其成为可能?

     #include<stdio.h>
     #include<stdlib.h>
     void main()
     {
     char *ptr, *new;
     ptr = "FECI00BS";
     strncpy(new,ptr,6);
     printf("%s",new);
     strcat(new,"NN");
     }

The above code is what I wrote. 上面的代码是我写的。 And I'm not sure why it is not working. 而且我不确定为什么它不起作用。 I understand that my requirement is very trivial, but I tried printfs in between. 我知道我的要求非常琐碎,但是我在两者之间尝试了printfs。 I was able to find that (ptr+6) printed "BS", so that 6 is the length that I need. 我能够找到(ptr + 6)打印的“ BS”,因此6是我需要的长度。 But this is still not working. 但这仍然行不通。 Any help appreciated. 任何帮助表示赞赏。 I need the output in a string pointer. 我需要字符串指针中的输出。 A new one is fine. 一个新的就可以了。 But a string pointer. 但是一个字符串指针。

PS: Only C code please. PS:请只使用C代码。 No C++. 没有C ++。

You didn't allocate memory to hold your new string 您没有分配内存来保存新字符串

char * new = calloc(1, 10); // on heap

or 要么

char new[10]; // on stack
memset(new, 0, sizeof(new)); // zero it

You're not allocating memory for storing the strings. 您没有分配用于存储字符串的内存。

You need to allocate memory for both *ptr and *new. 您需要为* ptr和* new分配内存。

The bare minimum would be: 最低要求是:

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

int main()
{

    char *ptr, *new;
    new = (char*)malloc(sizeof(char) * 9);
    new = (char*)malloc(sizeof(char) * 9);
    ptr = "FECI00BS";
    strncpy(a,ptr,6);
    printf("%s",new);
    strcat(a,"NN");
    printf("%s",new);

}

You need 9 bytes (assuming your platform needs one byte per char) because of the End-Of-Line character, '\\0', that needs to be present at the end of strings in order to use some string.h functions or print them correctly. 由于行尾字符'\\ 0',您需要9个字节(假设您的平台每个字符需要一个字节),为了使用某些string.h函数或print,该字符必须出现在字符串的末尾他们正确。

Also, read on why you need to be careful with strncpy . 另外,请阅读为什么需要小心使用strncpy Depending on your platform, you may have access to some newer, safer alternatives from the C standard. 根据您的平台,您可能可以使用C标准的一些更新,更安全的替代方法。

I think you want: 我想你要:

sprintf(ptr+6, "NN");

That will modify your buffer to convert BS into NN. 这将修改您的缓冲区以将BS转换为NN。 In this case you can get rid of the new variable. 在这种情况下,您可以摆脱new变量。

EDIT 编辑

Try this. 尝试这个。 Notice the char ptr[] instead of char* ptr . 注意char ptr[]而不是char* ptr By using [] instead of a pointer you are allocating the buffer on the stack. 通过使用[]而不是指针,您可以在堆栈上分配缓冲区。 This allows you to write to the buffer. 这使您可以写入缓冲区。

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

void main()
{
    char ptr[] = "FECI00BS";
    sprintf(ptr+6,"NN");
    printf(ptr);
}

Just a few simple checks and memcpy() . 只需进行一些简单的检查和memcpy()

char *ShivaAppend(char *dest, const char *ptr, const char *suffix) {
  size_t len = strlen(ptr);
  if (len != 8) {
    return NULL;
  }
  if (strcmp(ptr, "BS") != 0) {
    return NULL;
  }
  len = strlen(suffix);
  if (len != 2) {
    return NULL;
  }
  memcpy(dest, ptr, 6);
  memcpy(&dest[6], suffix, 2);
  return dest;
}

...
char dest[9];
char *p = ShivaAppend(dest, "SHIVA0BS", "NN")
puts(p == NULL ? "fail", p);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM