Characteristics of arrays and string ppt in c language
Size: 1.02 MB
Language: en
Added: Feb 17, 2024
Slides: 15 pages
Slide Content
Character Arrays and strings -Kishore Kumar
strings A string is a collection of characters that are enclosed within double quotes (" "). A string can also be called a character array.*It can be treated as a single data item and terminated by null character '\0’. C language does not support strings as a data type. A string is actually one - dimensional array of characters in C language. Example: The String "good" contains 5 characters including '\0' character which is automatically added by the compiler at the end of the string.
strings Declaring and initializing a string variable: There are different ways to initialize a character array variable Remember when you initialize a character array by listing all of its character separately then you must supply the ‘\0’ character explicitly
strings Some invalid ways to initialize a character array
String input and output: Input function scanf () can be used with %s format specifier to read a a string input from the terminal but there’s a problem with it, it terminates its input on the first whitespace it encounters However, C supports a format specification known as the edit set conversion code %[..] that can be used to read a line containing a variety of characters, including white spaces
String Handling Functions : C language supports a large number of string handling functions that can be used to carry out many of the string manipulations. The operations such as copying a string, joining two strings, extracting a portion of the string, determining the length of a string, etc., cannot be done using arithmetic operators. The string-based library functions are used to program this type of operations and they are located in the header file < string.h > , it should be included in the program to access the functions.
String handling functions : Method Description It is used to concatenate(combine) two strings strlen () It is used to show length of a string strrev () It is used to show reverse of a string strcpy () Copies one string into another strcmp () It is used to compare two strings strcat ()
String handling functions : We are not able to use + operator to join two strings. The function strcat ( ) is used for joining two strings. strcat (): Syntax : strcat (s1,s2); Example: Output: kishorekumar ;
String handling functions : t he function, strlen ( ) is used to find the length of the given string. strln (): Syntax : strlen (str); Example: Output: 5
String handling functions : The strrev ( ) function is used to reverse the given string expression. strrev (): Syntax : strrev (s1); Example: Output: Enter your string:Kishore Your reversed strings is erohisk
String handling functions : The function strcpy ( ) is used to copy the content of one string into another. strcpy (): Syntax : strcpy (s1); Example: Output: StudyTonight
String handling functions : The function is used to compare two strings. To compare any numeric values we use relational operators. By using these operators we cannot compare strings. The strcmp ( ) performs the function of comparison. strcmp (): Syntax : strcmp (s1,s2); NOTE: The result of comparison is obtained by calculating the difference between the ASCII values of the corresponding characters. Where s1 and s2 are string variables. This function returns any one of the three possible results. Result is 0 when both the strings are equal. (s1=s2) Result is Positive value, if s1 is greater than s2.(s1>s2) Result is Negative if s1 is less than s2.(s1<s2)
String handling functions : The function is used to compare two strings. To compare any numeric values we use relational operators. By using these operators we cannot compare strings. The strcmp ( ) performs the function of comparison. strcmp (): Example: Output: 1
rEad and write strings in c: Different methods to read and write strings in C: gets() and fgets () function are used to read the strings in C Strings in C can be displayed using puts() and fputs () functions and also printf () function can be used with %s format code fscant () and fprint () functions are used to get input and display the output in c respectively