Shiva S
Shiva S

Reputation: 33

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. (Ex: SHIVA0BS) And it is also a fact that the last two letters are always going to be "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?

     #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. I was able to find that (ptr+6) printed "BS", so that 6 is the length that I need. 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.

P.S: Only C code please. No C++.

Upvotes: 0

Views: 139

Answers (4)

John Sampson
John Sampson

Reputation: 574

I think you want:

sprintf(ptr+6, "NN");

That will modify your buffer to convert BS into NN. In this case you can get rid of the new variable.

EDIT

Try this. Notice the char ptr[] instead of 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);
}

Upvotes: 0

chux
chux

Reputation: 153367

Just a few simple checks and 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);

Upvotes: 0

ArthurChamz
ArthurChamz

Reputation: 2049

You're not allocating memory for storing the strings.

You need to allocate memory for both *ptr and *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.

Also, read on why you need to be careful with strncpy. Depending on your platform, you may have access to some newer, safer alternatives from the C standard.

Upvotes: 1

Sergey L.
Sergey L.

Reputation: 22542

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

Upvotes: 2

Related Questions