3

Im trying to scan a character from input , i have tried all method , scanf() getline() fgets() but all contains null terminator '\0' at the end which is unwanted for me . How can i strip it? I have tried

char *p=strtok(string,'\0\')

but it didnt work. How can i do it? I am scanning strings usgin getline() into an array of string , which works fine and i want to input an "needle" which i am trying to scanf with getline() too and than use strstr to find match but '\0' is causing no match

Abdir
  • 87
  • 1
  • 1
  • 6
  • 4
    What are you trying to achieve? – ouah Dec 07 '15 at 21:05
  • How about `getchar`? – Fiddling Bits Dec 07 '15 at 21:05
  • 1
    Generally, you should not remove the null terminating character from a string. If you do and you pass the string to any of the functions in string.h(and some functions in other libraries as well), the program will crash. – Tony Ruth Dec 07 '15 at 21:09
  • 1
    i need it to strip it bcs i am want to use this string as needle in strstr() and with '\0' it only matches the end of the string – Abdir Dec 07 '15 at 21:12
  • 1
    `strstr` requires both arguments to be NULL-terminated strings, so you shouldn't do that. Please post a [Minimal, Complete, Verifiable Example](http://stackoverflow.com/help/mcve) that illustrates the problem and your attempt at it. – dbush Dec 07 '15 at 21:14
  • 4
    You should explain your other problem because I think you're trying to fix it incorrectly. When using strings in C, the null terminator is required in order to know where the end of the string is. – lurker Dec 07 '15 at 21:18
  • 1
    The NULL terminator at the end of the string is not the cause of your problem. If you didn't have it, you'd have a different problem because `strstr` wouldn't know where to find the end of the string and might cause a core dump. Show us your code so we can see what you're really doing. – dbush Dec 07 '15 at 21:30
  • 1
    @Abdir: Strings in C always contain the '\0' character, because this character marks the end of the string. So, when functions that operate on strings read this character, they know that they are at the end of the string. All of what comes before '\0' is the content of the string. You won't have any problems using strstr; it's not true that it only matches the end of the sting. – Xaver Dec 07 '15 at 21:30
  • well it works like that in my code , i scan needle using getline , and it only matches char that are at the end of the string – Abdir Dec 07 '15 at 23:27
  • 1
    Your code isn't doing what you think it's doing. **Post the code**. – dbush Dec 07 '15 at 23:44
  • Found a [related question](http://stackoverflow.com/questions/34151059/cutting-0-from-strings). You might have an extra newline in your "haystack" that you don't see. – dbush Dec 08 '15 at 16:19

3 Answers3

3

You asked how to remove the NUL terminator from the end of a string. You can do that by overwriting it with something else (say, the letter 'a'):

str[strlen(str)] = 'a';

Note that C does not have a way to "remove" something from an array (including a string, which is an array of chars); array elements always exist (until the whole array is destroyed), and they can only be overwritten with other values.

user253751
  • 57,427
  • 7
  • 48
  • 90
2

C uses null-terminated strings which means that all library functions that are intended to work with "strings" will give you and expect to be given an array of characters follow by a null terminator.

You can copy all the bytes from the string except the null terminator into a new char array:

char buffer[strlen(string)];
memcpy(buffer, string, strlen(string));
p* = buffer;

Note: strlen doesn't include the null terminating character in the character count

But if you pass p* as parameter to a function that expects a string then expect problems at runtime. You've been warned :)

DonComo
  • 69
  • 8
  • Arrays in C must have a fixed size, unless they are allocated on the heap with malloc. The first line will throw an error. – qleguennec Dec 07 '15 at 21:44
  • 1
    @qleguennec That code snippet was intended to be used inside of a function and you can define a variable length array inside a function if you are using the C99 standard – DonComo Dec 07 '15 at 21:47
  • 1
    @qleguennec Declaring an array or a variable is still *"code"* and this can be done outside of a fuction. My code snippet obviously wouldn't work outside a function but when I wrote that comment I had in mind the line `char buffer[strlen(string)];` – DonComo Dec 07 '15 at 22:04
0

You could use srncpy with the size of your string to remove the '\0' at the end of your string.

char test[] = "example";
char *result;

result = malloc(sizeof(char) * strlen(test));
strncpy(result, test, strlen(test));

But I'm not sure if it's what you really want.

qleguennec
  • 544
  • 4
  • 12