answersLogoWhite

0

the character string is terminated by '\0'

User Avatar

Wiki User

13y ago

What else can I help you with?

Continue Learning about Engineering

How a string can be handled by array?

A string is an array. To be precise, a string is an array where every element is a character type. The term string simply relates to the fact the array (like any other type of array) resides in contiguous memory, thus forming a string of characters in memory.There are in fact two types of string array. The most common is the ASCII string, where each element is 1 byte in length (an array of type char). The other is the UNICODE string where each element is 2 bytes in length (an array of type wchar_t).In essence, ASCII strings are constructed from character codes 0-255 in the standard ASCII character set, while UNICODE can represent up to 65,536 different character codes, the first 256 being the same as the ASCII character set. UNICODE is generally used to provide character codes that are not available in ASCII, however many systems (Microsoft in particular) handle all strings as UNICODE strings (converting to and from ASCII as required). If you do a lot of string manipulation, then it is often best to use UNICODE at all times to reduce the amount of conversions going on in the background (check your compiler's documentation for specifics).When dealing with strings we often talk about string buffers. A buffer is simply an array of a specific size (allocated dynamically or statically) within which we can store a sequence of character codes. Since characters are consistently either one byte or two bytes long, it's easy to determine how many characters we can place in these buffers.However, just to confuse matters, there is actually a third type of string known as a variable-width encoded string. Microsoft calls these MBCS strings, but this really means multibyte character set, which is not the same. The width of a character is determined by the encoding, not the character set. The details don't really concern us, but suffice to say, each character has variable-width and there are many different encoding schemes available. As a general rule, if you must deal with variable-width encoded strings, use UNICODE instead, and only encode as variable width when you must. It'll make your life a lot simpler in the long run.You will undoubtedly encounter other types of string, such as std::string (and its UNICODE equivalent, std::wstring). This is really just an object that encapsulates a character array so you can use it just as you would a standard character array. However, do not let this fool you -- it is not a character array per se, it is merely a wrapper that mimics an array. There is an underlying array within the object's members, and several useful methods to manipulate the array (such as concatenating two strings), however memory management is handled by the object. While this simplifies things for programmers, it is not the most efficient way to store a string when that is all you want to do. But if you want to actually manipulate the string, then std:string is as good a way as any.To make use std:string you must include in your project. Do not confuse this with which merely declares a set of functions for manipulating c strings and arrays (the old fashioned way). This header is often called "cstring", which is often confused with the Microsoft-specific class CString (note the captialisation). CString can be likened to std:string in a lot of respects and crops up virtually everywhere in Microsoft objects. It is actually derived from the CStringT template which is itself derived from CSimpleStringT. It is therefore more complex, but the interface is extremely simple to use and if you do a lot of string manipulation, then CString can make life that much easier. But as it is not part of the standard language, its best avoided when code must be shared between platforms.


Difference between integer array and character array?

Arrays are basic structures wherein one or more elements exist "side by side" and are of the same "type". An "integer" array is an array whose elements are all of an integer type which has no fractional component. A "character" array is an array which contains nothing but character types. A "floating point" array contains elements that have both an integer and fractional portion. Simply put, they are arrays of particular types.


Why do you use ampersand symbol for arrays and not for a string?

You don't need to use ampersand for arrays; it's entirely optional even for strings (character arrays). This is because arrays will implicitly convert to a pointer at the slightest provocation. Thus for an array named X, you can either pass the array to a function as X, &X or &X[0], they all refer to the exact same address.


What do you mean by string in c language?

Character arrays or pointers to character are termed as strings in c language. Like:char arr[10] = {'s', 't', 'i', 'n', 'g'};char *pchar = "string";Above answer is the first answer for the questionBut there is a lot of difference between character array and string.string means a group of characters .And string is enclosed between double quotation marks i.e(" ") .Declaration of string is same as of char array ,which is as followschar str[20];And initialization is different from that of character arrayinitialization:-char str[7]={"vardhan"};


Manupulating strings and arrays together in c plus plus?

A string is an array; an array of character data types (char or wchar_t). Therefore anything you can do with an array you can also do with a string. C does not have a built-in type for either an array or a string, but C++ does. In C, the programmer was entirely responsible for managing the memory allocated to an array. Built-in functions allowed us to determine the length of a null-terminated string, append strings, or alter the amount of memory allocated to an array, but the functions and the arrays were entirely separate. With C++, std::vector and std::string objects allow us to manipulate dynamic arrays and variable length strings, respectively, without having to worry about the underlying memory allocations. Since all the required methods are encapsulated within the objects themselves, they are much easier to work with. For instance, in C, if we wanted to determine the length of a string we might call the strlen function: char* str = "Hello world!"; unsigned size = strlen (str); Arrays are a bit more difficult in that we must maintain a separate variable to keep track of an array's length: unsigned size = 10; int* arr = malloc(size); size = 11; arr = realloc(arr, size); In C++, strings and arrays know their own length, so we don't need to call external functions or maintain separate variables: std::string str = "Hello world!"; std::cout << '"' << str << '"' << " has " << str.size() << " characters\n"; std::vector<int> vect(10,0); std::cout << "vect has " << vect.size() << " elements\n"; A vector of strings allows us to manipulate strings and vectors together. A vector of strings is essentially a two-dimensional dynamic array where each row is a string, and every string can have different lengths. The following therefore creates a vector with 10 empty strings: std::vector< std::string > vstrings (10, ""); We can then manipulate each of the individual strings in the array: vstrings[0] = "Hello world!"; vstrings[1] = "Another string."; vstrings[2] = "The third string."; ... vstrings[9] = "The string to end all strings."; Each of these strings is a different length. With a traditional two-dimensional array each row would be the same length. In this case we'd need at least a 10 x 31 array (including null-terminators), which wastes memory when any string is less than 30 characters long. The only way to resolve this is to use a one-dimensional array of pointers to strings instead. Vectors of strings do the same thing, but they are much easier to work with since all memory management is handled by the objects themselves.

Related Questions

What is array of string?

An array of strings is usually implemented as an array of character pointers. Each pointer refers to a a null-terminated character array, and can be treated just as if it were a two-dimensional array where the length of each "row" is not fixed length (the null terminator marks the end of each row). The array of character pointers must be allocated in contiguous memory (as must all one-dimensional arrays), however the character arrays they point to need not be allocated contiguously with each other (only the individual one-dimensional character arrays must be contiguous).


What String of what?

An array of strings is usually implemented as an array of character pointers. Each pointer refers to a a null-terminated character array, and can be treated just as if it were a two-dimensional array where the length of each "row" is not fixed length (the null terminator marks the end of each row). The array of character pointers must be allocated in contiguous memory (as must all one-dimensional arrays), however the character arrays they point to need not be allocated contiguously with each other (only the individual one-dimensional character arrays must be contiguous).


How a string can be handled by array?

A string is an array. To be precise, a string is an array where every element is a character type. The term string simply relates to the fact the array (like any other type of array) resides in contiguous memory, thus forming a string of characters in memory.There are in fact two types of string array. The most common is the ASCII string, where each element is 1 byte in length (an array of type char). The other is the UNICODE string where each element is 2 bytes in length (an array of type wchar_t).In essence, ASCII strings are constructed from character codes 0-255 in the standard ASCII character set, while UNICODE can represent up to 65,536 different character codes, the first 256 being the same as the ASCII character set. UNICODE is generally used to provide character codes that are not available in ASCII, however many systems (Microsoft in particular) handle all strings as UNICODE strings (converting to and from ASCII as required). If you do a lot of string manipulation, then it is often best to use UNICODE at all times to reduce the amount of conversions going on in the background (check your compiler's documentation for specifics).When dealing with strings we often talk about string buffers. A buffer is simply an array of a specific size (allocated dynamically or statically) within which we can store a sequence of character codes. Since characters are consistently either one byte or two bytes long, it's easy to determine how many characters we can place in these buffers.However, just to confuse matters, there is actually a third type of string known as a variable-width encoded string. Microsoft calls these MBCS strings, but this really means multibyte character set, which is not the same. The width of a character is determined by the encoding, not the character set. The details don't really concern us, but suffice to say, each character has variable-width and there are many different encoding schemes available. As a general rule, if you must deal with variable-width encoded strings, use UNICODE instead, and only encode as variable width when you must. It'll make your life a lot simpler in the long run.You will undoubtedly encounter other types of string, such as std::string (and its UNICODE equivalent, std::wstring). This is really just an object that encapsulates a character array so you can use it just as you would a standard character array. However, do not let this fool you -- it is not a character array per se, it is merely a wrapper that mimics an array. There is an underlying array within the object's members, and several useful methods to manipulate the array (such as concatenating two strings), however memory management is handled by the object. While this simplifies things for programmers, it is not the most efficient way to store a string when that is all you want to do. But if you want to actually manipulate the string, then std:string is as good a way as any.To make use std:string you must include in your project. Do not confuse this with which merely declares a set of functions for manipulating c strings and arrays (the old fashioned way). This header is often called "cstring", which is often confused with the Microsoft-specific class CString (note the captialisation). CString can be likened to std:string in a lot of respects and crops up virtually everywhere in Microsoft objects. It is actually derived from the CStringT template which is itself derived from CSimpleStringT. It is therefore more complex, but the interface is extremely simple to use and if you do a lot of string manipulation, then CString can make life that much easier. But as it is not part of the standard language, its best avoided when code must be shared between platforms.


Difference between integer array and character array?

Arrays are basic structures wherein one or more elements exist "side by side" and are of the same "type". An "integer" array is an array whose elements are all of an integer type which has no fractional component. A "character" array is an array which contains nothing but character types. A "floating point" array contains elements that have both an integer and fractional portion. Simply put, they are arrays of particular types.


Why do you use ampersand symbol for arrays and not for a string?

You don't need to use ampersand for arrays; it's entirely optional even for strings (character arrays). This is because arrays will implicitly convert to a pointer at the slightest provocation. Thus for an array named X, you can either pass the array to a function as X, &X or &X[0], they all refer to the exact same address.


How many strings of five ASCII characters contain the character at least once?

the total 128 ^5 the strings without @ at all 127^5 to get the strings that has at least @ once 128^5 - 127^5


What do you mean by string in c language?

Character arrays or pointers to character are termed as strings in c language. Like:char arr[10] = {'s', 't', 'i', 'n', 'g'};char *pchar = "string";Above answer is the first answer for the questionBut there is a lot of difference between character array and string.string means a group of characters .And string is enclosed between double quotation marks i.e(" ") .Declaration of string is same as of char array ,which is as followschar str[20];And initialization is different from that of character arrayinitialization:-char str[7]={"vardhan"};


Manupulating strings and arrays together in c plus plus?

A string is an array; an array of character data types (char or wchar_t). Therefore anything you can do with an array you can also do with a string. C does not have a built-in type for either an array or a string, but C++ does. In C, the programmer was entirely responsible for managing the memory allocated to an array. Built-in functions allowed us to determine the length of a null-terminated string, append strings, or alter the amount of memory allocated to an array, but the functions and the arrays were entirely separate. With C++, std::vector and std::string objects allow us to manipulate dynamic arrays and variable length strings, respectively, without having to worry about the underlying memory allocations. Since all the required methods are encapsulated within the objects themselves, they are much easier to work with. For instance, in C, if we wanted to determine the length of a string we might call the strlen function: char* str = "Hello world!"; unsigned size = strlen (str); Arrays are a bit more difficult in that we must maintain a separate variable to keep track of an array's length: unsigned size = 10; int* arr = malloc(size); size = 11; arr = realloc(arr, size); In C++, strings and arrays know their own length, so we don't need to call external functions or maintain separate variables: std::string str = "Hello world!"; std::cout << '"' << str << '"' << " has " << str.size() << " characters\n"; std::vector<int> vect(10,0); std::cout << "vect has " << vect.size() << " elements\n"; A vector of strings allows us to manipulate strings and vectors together. A vector of strings is essentially a two-dimensional dynamic array where each row is a string, and every string can have different lengths. The following therefore creates a vector with 10 empty strings: std::vector< std::string > vstrings (10, ""); We can then manipulate each of the individual strings in the array: vstrings[0] = "Hello world!"; vstrings[1] = "Another string."; vstrings[2] = "The third string."; ... vstrings[9] = "The string to end all strings."; Each of these strings is a different length. With a traditional two-dimensional array each row would be the same length. In this case we'd need at least a 10 x 31 array (including null-terminators), which wastes memory when any string is less than 30 characters long. The only way to resolve this is to use a one-dimensional array of pointers to strings instead. Vectors of strings do the same thing, but they are much easier to work with since all memory management is handled by the objects themselves.


What is c plus plus data type?

The C++ string classes are derived from the std::basic_string<T> template class, where T is a character type. The standard provides the two most common string types, std::string (std::basic_string<char>) and std::wstring (std::basic_string<wchar_t>). The former is used for ASCII strings (which includes UTF-8 strings) while the latter is used for wide-character strings, particularly UNICODE strings. All standard library strings are represented by an array of some character type (such as char or wchar_t) however, unlike an ordinary array, the individual character elements cannot be re-ordered other by overwriting each character. If you simply need an array of characters that can be re-ordered then use a std::vector<char> or std::vector<wchar_t> rather than a string. The standard library strings include a rich set of methods, operations and algorithms that are commonly used with strings, such as std::string::find() to locate a character within a string, std::string::substr() to extract a substring from a string, and operator+= to concatenate one string onto another. Most implementations include the short string optimisation so that short strings (up to around 14 characters or so) may be stored in local memory rather than on the heap. Many strings tend to be short so this can provide enormous performance benefits with little cost in terms of memory.


Character string in c are atumatically terminated by the null character explain how this feature help in string manipulation?

A string is an array of type char. Like all arrays, a string implicitly decays to a pointer thus there is no way to determine the length of the array from the pointer alone. Although some languages store the string's length in the first element of the string, this effectively limits ASCII strings to a maximum of 255 characters, the maximum value that can be stored in an 8-bit integer. With null-terminated strings, the only limit to the length of a string is available memory. Calculating the length of a string is an O(n) operation with the following optimal implementation: unsigned strlen (char* pstr) { unsigned len = 0; while (*pstr!='\0') ++len, ++pst; return len; } The triviality of this function means it can be inline expanded, so we don't pay the cost of calling and returning from the function. Extensive testing has shown that the vast majority of strings tend to be very short (less than 14 chars or so), so the cost of calculating string lengths is relatively low. However, if we need to make use of the length in several operations, it is best to calculate the length one time only and store the result. The null-terminator marks the end of a string, but does not necessarily mark the end of the character array. This makes it possible to compactly store two or more strings in the same character array.


How do you write a quiz using files and struct?

There are many ways. The simplest way would be to use a structure (Q) with two members: a pointer to the question and a pointer to the answer. If the quiz is text-based, then both pointers will point to null-terminated strings. If the quiz is multiple choice, then the answer may point to another structure (A) containing a dynamic array of pointers to null-terminated strings, along with a zero-based index to the correct answer. Either way, you can build a series of questions using a one-dimensional array of Q structures. To store the data in a file, you simply store the count of questions (the number of elements in the Q array) and then sequentially write all of the questions followed by all of the answers. If the answers are a multiple choice structure (A), then store the number of elements in the answer array, the zero-based index of the correct answer, and then the answer array itself. When you read the file back, you can rebuild the original array. Of course anyone can read the text file outwith your program and cheat the quiz. To avoid this, obfuscate the answers by XOR-ing each character (other than the NULL terminator) with a pre-determined, nonzero character code. When you read the file back, XOR the characters with that same character code to restore the original characters. This is a simplistic cipher but it will be enough to deter most would-be cheaters. can you please give me your email or contact me please as soon as possible nabeelbhutto@gmail.com


What do array elements all have in common?

They are all of the same type and they all have an index position that signifies their location in the array