answersLogoWhite

0

null character exists at the end of the string.It denotes the end of it.

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

Why strcat(string'!') not work in C program?

The strcat() function has the following protocol:char* strcat (char* destination, char* source);The function appends the source string to the destination string and returns the destination string.The destination string must be a null-terminated character array of sufficient length to accommodate strlen (source) plus strlen (destination) characters, plus a null-terminator. The existing null-terminator and subsequent characters of destination are overwritten by characters from the source string, up to and including the source string's null-terminator.strcat (string, '!') will not work because '!' is a character literal (ASCII code 33 decimal), not a null-terminated character array. Use "!" instead of '!'.Example:char string[80]; // character arraystrcpy (string, "Hello world");strcat (string, "!");puts (string);


How are string variables stored?

Typically as null-terminated character arrays. However, some languages use the first element of the array to store the length of the string rather than a null-terminator to mark the end of the string.


How you work character type data in array c plus plus?

It's not clear from the question what you mean by "work". However character data types (char and wchar_t) are intended to store character codes and they work exactly the same whether as a single variable or as an array of characters. If you want to use the array as a string, however, remember to include a null-terminator at the end of the string.


How do you write a c program for to replace a character of string either fro mbeginning or ending or at specified location?

The standard C library includes two simple utilities to find the first or last occurance of a given character within a given string, strchr() to search from the start and strrchr() for the reverse start from the end. Subject to the chosen search direction, you could use one of these two simple API. Both return a pointer to the location of the matching character within the string, or NULL if no such character is found. Note that this approach assumes a mutable string, a string stored in writeable memory. A string literal is a constant string and not generally mutable (even though some compilers are very casual about this). That is, strchr("the quick brown fox", 'q') will return a pointer to the first 'q', but since the string is a string of constant characters, you shouldn't use the pointer to change the letter found. To search and modify, you'd use string of variable characters, such as one allocated with the malloc() or strdup() standard API, or one created as a char array.


How do you write a function that counts the number of characters in a string?

As this is probably a homework question, I will give you some pseudo code: [code] num_chars = 0 READ ch FROM string WHILE ch IS NOT END OF STRING num_chars = num_chars + 1 READ ch FROM string END WHILE [/code] Remember that in C, we use what are called "C-strings". C-strings are a pointer to a continuous group of characters in memory which are terminated by a null character. The null character is '\0', and has an integer value of 0. The C-string generally points to the first character in the string. To access the value of this character, you must use the dereferencing operator, *. If you want to move to the next character, you simply add 1 to the pointer. So if you have a C-string: char *str = "abcd"; then: *str '\0' Anything past the null character is undefined. Trying to access this data is considered to be a buffer overflow, and is very dangerous. Note that c-strings created as pointers should always be treated as immutable, as trying to change them might produce errors. Many compilers will allocate the above string inside the static data area, along with any constants or literals which can not fit inside the immediate field of an instruction. If you want a mutable string, then declare it as a character array: char str[] = "abcd"; This method of declaration will explicitly allocate memory on the stack to store the c string in, and as such, the string can be safely manipulated without fear of unintended side effects.

Related Questions

Why strcat(string'!') not work in C program?

The strcat() function has the following protocol:char* strcat (char* destination, char* source);The function appends the source string to the destination string and returns the destination string.The destination string must be a null-terminated character array of sufficient length to accommodate strlen (source) plus strlen (destination) characters, plus a null-terminator. The existing null-terminator and subsequent characters of destination are overwritten by characters from the source string, up to and including the source string's null-terminator.strcat (string, '!') will not work because '!' is a character literal (ASCII code 33 decimal), not a null-terminated character array. Use "!" instead of '!'.Example:char string[80]; // character arraystrcpy (string, "Hello world");strcat (string, "!");puts (string);


How do you use the strtok function?

strtok sequentially truncate string if delimiter is found. If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter. After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter. delimiters may vary from a call to another. Parameters. string Null-terminated string to scan. separator Null-terminated string containing the separators. Return Value. A pointer to the last token found in string. NULL is returned when there are no more tokens to be found. Portability. Defined in ANSI-C.


How are string variables stored?

Typically as null-terminated character arrays. However, some languages use the first element of the array to store the length of the string rather than a null-terminator to mark the end of the string.


How strings and characters are represented in an array?

An array of characters is an array of character codes (such as ASCII codes). A string is typically a null-terminated array of characters however some languages use the first array element to specify the string's length.


Program to find the size of the string without using string functions?

You can't. If you want to find the length of a String object, you must use at least one of the String methods. Simply iterate over your char* and count the number of characters you find before you reach the null character . int strLength(const char* str) { int length = 0; // take advantage of the fact that all strings MUST end in a null character while( str[length] != '\0' ) { ++length; } return length; }


Why we don't use NULL character in array?

Because it is not a character, it is a pointer. Anyway, the following is perfectly legal: char str [4] = { 'A', 'B', 'C', (char)NULL};


How you work character type data in array c plus plus?

It's not clear from the question what you mean by "work". However character data types (char and wchar_t) are intended to store character codes and they work exactly the same whether as a single variable or as an array of characters. If you want to use the array as a string, however, remember to include a null-terminator at the end of the string.


Is 'b' or b a character literal?

'b' is a character literal. A character literal is a character enveloped in single quotes, just as a String literal is a String enveloped in double quotes (without the use of a constructor.)


How do you reverse a given string in c plus plus?

The simplest way is to use the strrev() function.To manually reverse a string, point to the first and last characters, swap their values, then move the pointers one character towards the middle of the string and repeat. If the pointers point to the same character or pass each other, the string is reversed.The following is an example implementation. The function only process the given string when the string has 2 or more characters and a null-terminator is found within the first lencharacters.#include #include using namespace std;void strRev( char * input, size_t len ){// Minimum length is three (2 characters, 1 null-terminator).const int min = 3;if( len < min )return;// Locate the left-most character.char * l = input;// Locate the null-terminator:char * n = l;while( *n && n < l+len )++n;if( *n )return; // null-terminator not found.// Confirm the length (ignoring null-terminator).if( n-l < min-1 )return;// Locate the right-most character (left of null-terminator)char * r = n-1;// Repeat while right pointer is greater than left pointer.while( r>l ){*n = *l; // use the null-terminator to assist the swap.*l++ = *r; // change left value and advance pointer.*r-- = *n; // change right value and retreat pointer.}// Restore the null terminator.*n = 0;}int main(){// Safest method of entering a string:string input = "";printf( "Enter a string: ");getline( cin, input );// Determine length of input + null-terminator.size_t len = input.length() + 1;// Convert string to a null-terminated string.char * p = ( char * ) calloc( len, sizeof( char ));memcpy( p, input.data(), input.length() );// Reverse the string.strRev( p, len );printf( "Reversed: %s\n", p );// Release memory.free( p );return( 0 );}


What menu command do you use to locate occurrrences of a character string string?

either by using find or find and replace


How do you write a c program for to replace a character of string either fro mbeginning or ending or at specified location?

The standard C library includes two simple utilities to find the first or last occurance of a given character within a given string, strchr() to search from the start and strrchr() for the reverse start from the end. Subject to the chosen search direction, you could use one of these two simple API. Both return a pointer to the location of the matching character within the string, or NULL if no such character is found. Note that this approach assumes a mutable string, a string stored in writeable memory. A string literal is a constant string and not generally mutable (even though some compilers are very casual about this). That is, strchr("the quick brown fox", 'q') will return a pointer to the first 'q', but since the string is a string of constant characters, you shouldn't use the pointer to change the letter found. To search and modify, you'd use string of variable characters, such as one allocated with the malloc() or strdup() standard API, or one created as a char array.


How do you write a function that counts the number of characters in a string?

As this is probably a homework question, I will give you some pseudo code: [code] num_chars = 0 READ ch FROM string WHILE ch IS NOT END OF STRING num_chars = num_chars + 1 READ ch FROM string END WHILE [/code] Remember that in C, we use what are called "C-strings". C-strings are a pointer to a continuous group of characters in memory which are terminated by a null character. The null character is '\0', and has an integer value of 0. The C-string generally points to the first character in the string. To access the value of this character, you must use the dereferencing operator, *. If you want to move to the next character, you simply add 1 to the pointer. So if you have a C-string: char *str = "abcd"; then: *str '\0' Anything past the null character is undefined. Trying to access this data is considered to be a buffer overflow, and is very dangerous. Note that c-strings created as pointers should always be treated as immutable, as trying to change them might produce errors. Many compilers will allocate the above string inside the static data area, along with any constants or literals which can not fit inside the immediate field of an instruction. If you want a mutable string, then declare it as a character array: char str[] = "abcd"; This method of declaration will explicitly allocate memory on the stack to store the c string in, and as such, the string can be safely manipulated without fear of unintended side effects.