A null pointer is a pointer which does not point to any valid memory location, and usually contains the binary value "0" to represent this (this is language dependent). The ASCII null character is a character-sized zero value (in ASCII, it is an unsigned byte with a value of 0), and typically represents the end of a string (esp. as in C and C++). A null string is one that is zero characters of usable string data; in a length-based string, this means the length parameter is set to 0, and in an ASCII null-terminated string, means the first character is set to 0.
char *ptr;
In C, you can assign integers multiple characters such that they fit in their size. For e.g.: int - 4 bytes char - 1 byte So an assignment like this is valid: int a = 'ABCD'; The first byte in a will be assigned the value of 'A', the second - 'B' and so on. A string literal is a character array constant. It is enclosed in double quotes and assignment can only be made to a char pointer. There is no limit on the size of the literal and it is terminated with a null character. e.g.: char str[] = "This is a trial";
C SolutionTo replace a single, specified character with another in a given string, one possibility is ...char *pszString; /* pointer to string */int offset; /* offset of desired character */... initialize pszString and offset*(pszString+offset) = 'A'; /* or whatever new value you want */Obviously, this is a simple example, and it does not consider if offset is greater than the size of the array.If you want to replace every occurence of a character with another, here is another possibility, one that also handles string length ...char *pszString; /* pointer to string */char* pszTemp; /* temporary scanning pointer */char cOldChar; /* character to change */char cNewChar; /* new character */... initialize pszString, cOldChar, and cNewCharfor (pszTemp = pszString; *pszTemp != '\0'; pszTemp++) { /* scan */if (*pszTemp == cOldChar) *pszTemp = cNewChar; /* conditionally replace */}Java Solution// Replace all 'e' characters with 'i' characters in String strstr.replaceAll("e", "i");
sprintf: This Writes formatted data to a character string in memory instead of stdoutSyntax of sprintf is:#include int sprintf (char *string, const char *format[,item [,item]...]);HereString refers to the pointer to a buffer in memory where the data is to be written. Format refers to pointer to a character string defining the format. Each item is a variable or expression specifying the data to write.The value returned by sprintf is greater than or equal to zero if the operation is successful or in other words the number of characters written, not counting the terminating null character is returned. And return a value less than zero if an error occurred.printf: Prints to stdoutSyntax for printf is:printf format [argument]...The only difference between sprintf() and printf() is that sprintf() writes data into a character array, while printf() writes data to stdout, the standard output device
In C programming, a string doesn't have a specific return type as it's essentially an array of characters. So, if a function is returning a string, it should be declared to return a pointer to a char (char*), since a string in C is represented as an array of characters terminated by a null character ('\0').
A string ends with a '\0' character,but character is not.
Well, A is an identifier; 'A' is a character-literal; "A" is a string literal (of 1 character); "'A'" is another string literal (of 3 characters).
char *ptr;
a -- identifier 'a' -- character-literal "a" -- string-literal
int main (int argc, char **argv):Hereargv is a pointer to a pointer (points to the first element of a pointer-array)argv[0] is a pointer (points to the first character of a string)argv[0][0] is a character
Nothing Most computer languages have a string-termination character that is "invisible" to people. So, while a character variable may contain 'A', and a string variable appears to be simply "A", the string variable will actually have two characters. This difference is often masked by compilers and languages, but it exists nonetheless, and it is sloppy programming practice to compare a string to a character (or vice versa) without doing a type cast.
A character type constant is a character, a char, while a string type is an array of characters. The character type constant is one character and is delimited by single quotes. The string type constant is zero or more characters and is delimited by double quotes.
In C, you can assign integers multiple characters such that they fit in their size. For e.g.: int - 4 bytes char - 1 byte So an assignment like this is valid: int a = 'ABCD'; The first byte in a will be assigned the value of 'A', the second - 'B' and so on. A string literal is a character array constant. It is enclosed in double quotes and assignment can only be made to a char pointer. There is no limit on the size of the literal and it is terminated with a null character. e.g.: char str[] = "This is a trial";
I guess by 'string' you mean a character-array; so the answer is: an array in itself is a pointer to its first element: arr==&arr[0]. (Note: it is a pointer-constantant, not a pointer-variable, so you cannot change it's value: 'arr= something' is wrong.)
There is no difference. A string is just an array of type char. The only real difference is that we do not need to keep track of the length of a string because strings are null-terminated in C. If a string does not have a null-terminator, then it is just an ordinary array of character values.
Simply put, a pointer is a memory address. It "points" to data at that address. The Data Type of the pointer tells the program how to interpret the data at the address. For example, a character pointer will read the data at the address as a byte and interpret it as a character (like an ASCII character, for instance). A string pointer is just like a character pointer, except that it starts reading characters at the address and keeps going until it encounters a null (a data value of zero). The "string" of characters is interpreted as text. The available data types include just about anything you can imagine. Intrinsic types, structures, functions... in later versions of C, pointers can point to objects.
C SolutionTo replace a single, specified character with another in a given string, one possibility is ...char *pszString; /* pointer to string */int offset; /* offset of desired character */... initialize pszString and offset*(pszString+offset) = 'A'; /* or whatever new value you want */Obviously, this is a simple example, and it does not consider if offset is greater than the size of the array.If you want to replace every occurence of a character with another, here is another possibility, one that also handles string length ...char *pszString; /* pointer to string */char* pszTemp; /* temporary scanning pointer */char cOldChar; /* character to change */char cNewChar; /* new character */... initialize pszString, cOldChar, and cNewCharfor (pszTemp = pszString; *pszTemp != '\0'; pszTemp++) { /* scan */if (*pszTemp == cOldChar) *pszTemp = cNewChar; /* conditionally replace */}Java Solution// Replace all 'e' characters with 'i' characters in String strstr.replaceAll("e", "i");