answersLogoWhite

0

There is no need to remove any characters, simply overwrite the first character with a null terminator.

Example:

#include<stdio.h>

void clear_string (char* pstr) {

if (pstr!=0) pstr[0]='\0';

}

int main (void) {

char str[] = "Hello world!";

printf ("\n%s", str); // prints Hello world! on a new line clear_string (str);

printf ("\n%s", str); // prints an empty string

return 0;

}

Note that the character array, str, will effectively contain the character sequence "\0llo World!\0", however the first null-terminator marks the end of the string, so the rest is simply ignored. There is no point in shrinking an array (even a string) unless you absolutely have to, but even then you can only do that with strings allocated on the heap, like so:

#include<stdio.h>

void clear_heap_string (char** ppstr) {

if (ppstr==0 *ppstr==0) return;

char* pstr = realloc (*ppstr, 1 * sizeof(char));

if (pstr==0) return;

pstr[0] = '\0';

*ppstr = pstr;

}

int main (void) {

char* pstr = malloc (13 * sizeof(char));

*pstr = "Hello world!";

printf ("\n%s", pstr); // prints Hello world! on a new line clear_heap_string (&pstr);

printf ("\n%s", pstr); // prints an empty string

free (pstr);

return 0;

}

Note that if the reallocation fails, pstr will still be pointing at the Hello world! string. For that reason it is simpler to just overwrite the first character with a null-terminator, which is always guaranteed to succeed.

User Avatar

Wiki User

9y ago

What else can I help you with?

Related Questions

What is the constant string length method?

To get the length of the string we use length property. The length property returns the length of a string (number of characters we use).If the string is empty, length of the string will be 0. Syntax: string.length Hope this helps.


How do you construct a syntax- directed translator that verifies that the parenthesis in an input string are properly balance?

How do you construct a syntax- directed translator that verifies that the parenthesis in an input string are properly balance?


What is syntax of memcpy?

memcpy function is used to copy memory area. Syntax ------ void *memcpy(void *dest, const void *src, size_t n); *dest is a destination string. *src is a source string. n is a number of characters to be copied from source string. Example: #include #include main() { char src[]="Hello World"; char des[10]; memcpy(des,src,5); printf("des:%s\n",des); //It will contain the string "Hello". }


Is it possible to initialize null character in string?

yes we can initialize null characterfor example syntax :string='\0';


What elements are in string?

A string is an array of characters.


How do you Print all the first characters in the string in python programming?

Let's say your string is a variable called "string" To print out all the characters in order, you would do: for i in string: print(string[i]) If you wanted to print out characters up to a point (n = maximum characters): for i in range(n): print(string[i]) hope this helps!


Are spaces taken as single character in a string?

public class count { public static void main(String[] args) { String string = "1 2 3 4"; char[] array = string.toCharArray(); System.out.println("Number of characters in string: " + string.length()); System.out.println("Number of characters in array: " + array.length); } } Output: Number of characters in string: 7 Number of characters in array: 7 So yes, spaces are taken as single characters in a string.


How do you count the number of words in a string?

To find the length of the string we use length method. The length property returns the length of a string (number of characters we use).The length of an empty string is 0. For example: function myFunction() { var str = &quot;Hello World!&quot;; var n = str.length; Hope this hepls.


Count characters in an input string using c sharp?

Console.WriteLine("Please input a string:"); string str = Console.ReadLine(); Console.WriteLine("Number of characters: " + str.Length);


How do you get value of a string in reverse order in c?

Assume C#, not C: Traditional way: public string Reverse(string s) { if (string.IsNullOrEmpty(s)) return s; // "" or null char[] characters = s.ToCharArray(); Array.Reverse(characters); return new string(characters); } or as an extension method: public static string Reverse(this string s) { if (s == "") return ""; char[] characters = s.ToCharArray(); Array.Reverse(characters); return new string(characters); } The differences of the 2 methods above is on the caller (how to use Reverse()), and they may co-exist: For example: string test = "abc"; string result1 = Reverse(test); // traditional way string result2 = test.Reverse(); // call the extension


What is in string?

A string is a collection of words or characters in '' or "" it is also a data type.


How can you define and manipulate a string scalar in programming languages?

In programming languages, a string scalar is a sequence of characters. To define a string scalar, you enclose the characters in quotation marks. To manipulate a string scalar, you can perform operations like concatenation (joining strings together), slicing (extracting a portion of the string), and searching for specific characters or substrings within the string.