Is it possible to set an index of a char array (string) to NULL.
I know that you cant do the following:
Is there a way to set buffer[4] = ''; or null?Code:int MAXBUFFERSIZE] = 5 char buffer[MAXBUFFERSIZE]; buffer[4] = 't'; buffer[4] = NULL;
Is it possible to set an index of a char array (string) to NULL.
I know that you cant do the following:
Is there a way to set buffer[4] = ''; or null?Code:int MAXBUFFERSIZE] = 5 char buffer[MAXBUFFERSIZE]; buffer[4] = 't'; buffer[4] = NULL;
If you have a string "abcdef" and you want to remvoe 'c' from the string, you need to "cut it out" by copying the first part of the string, skipping the c and then concatenating the rest onto the first part.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
You could make a linked list to store your string in. (Every node stores a char of the string).
Operating Systems:
- Ubuntu 9.04
- XP
Compiler: gcc
I guess moving a couple of characters from one place to another is simpler than doing a linked list (you'd also need to reimplement all string functions).
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Assigning NULL as above is incorrect. NULL is a null pointer constant, and not the null character. It's possible that on some systems, depending on how NULL is defined, that will work and do what's expected, but it's not guaranteed. To represent the null character, use either 0 or '\0'.Code:char buffer[MAXBUFFERSIZE]; buffer[4] = 't'; buffer[4] = NULL;
Just changing NULL to 0 won't solve your problem, but I wanted to point out this (somewhat common) misconception.
Edit: I realize that you knew the above wouldn't be a solution to your problem; but I'm not sure if you understood why.
Last edited by cas; 04-23-2009 at 01:43 PM. Reason: Clarification.
>Is it possible to set an index of a char array (string) to NULL.
Well, it is possible. But you would achieve what you wanted. Like you wanted to skip that index value.
Instead it would just print string up tp the '\0' char. Here is an example
-ssharishCode:#include <stdio.h> int main() { char str[] = { "Its a bit obvious." }; printf("Before placing NULL : %s\n", str); str[10] = '\0'; printf("After placing NULL : %s\n", str); getchar(); return 0; } /* my output Before placing NULL : Its a bit obvious. After placing NULL : Its a bit */
Life is like riding a bicycle. To keep your balance you must keep moving - Einstein
If you want to delete a character from a string you can try these handy functions I found in a book:
Code:$ cat testdel.c #include <stdio.h> #include <stdlib.h> #include <string.h> void strdelcn(char *s, int n, char c); void strdelc(char *s, char c); int main () { char teststr[100]; memset(teststr, 0, sizeof(teststr)); strcpy(teststr, "This is my test string."); printf("teststr before strdelcn [%s], len[%d]\n", teststr, (int) strlen(teststr)); strdelcn(teststr, 1, 's'); printf("teststr after strdelcn [%s], len[%d]\n", teststr, (int) strlen(teststr)); strcpy(teststr, "This is my test string."); printf("teststr before strdelc [%s], len[%d]\n", teststr, (int) strlen(teststr)); strdelc(teststr, 's'); printf("teststr after strdelc [%s], len[%d]\n", teststr, (int) strlen(teststr)); exit (0); } /***** strdelcn() This function deletes the specified number of occurrences of a given character given from a string. Argument list: char *s the string to be searched int n the number of occurrences to delete char c the character to be deleted Return value: void *****/ void strdelcn(char *s, int n, char c) { while (*s) { if (*s == c && n) { strcpy(s, s + 1); n--; } else { s++; } } } /***** strdelc() This function deletes all occurrances of a character from a string. Argument list: char *s the string to be searched char c the character to be deleted Return value: void *****/ void strdelc(char *s, char c) { while (*s) { if (*s == c) { strcpy(s, s + 1); } else { s++; } } } $ ./testdel.exe teststr before strdelcn [This is my test string.], len[23] teststr after strdelcn [Thi is my test string.], len[22] teststr before strdelc [This is my test string.], len[23] teststr after strdelc [Thi i my tet tring.], len[19]
Unfortunately, this is not valid code. You cannot use strcpy() with overlapping objects. It may work on some systems, depending on how strcpy() is implemented, but there are no guarantees.If you want to delete a character from a string you can try these handy functions I found in a book:
Code:... strcpy(s, s + 1); ...
You can however use memmove.
Quzah.
Hope is the first step on the road to disappointment.