Lecture 9
Lecture 9
Lecture 9
CSE113
1
String
String
Strings are used for storing text/characters.
For example, "Hello World" is a string of characters.
Unlike many other programming languages, C does not have a
String type to easily create string variables. Instead, you must use
the char type and create an array of characters to make a string in C.
So, The string can be defined as the one-dimensional array of
characters terminated by a null ('\0'). The character array or the
string is used to manipulate text such as word or sentences. Each
character in the array occupies one byte of memory, and the last
character must always be 0.
The termination character ('\0') is important in a string since it is the
only way to identify where the string ends. When we define a string
as char s[10], the character s[10] is implicitly initialized with the
null in the memory.
String Declaration
There are two ways to declare a string in c language.
• By char array
• By string literal
Example of declaring string by char array in C language.
char ch[12]={‘P', ’r', ’o', ’g', ’r', ’a', ’m', ’m', ’i', ’n', ’g’, '\0'};
As we know, array index starts from 0, so it will be represented as
in the figure given below
0 1 2 3 4 5 6 7 8 9 10 11
P r o g r a m m i n g \0
• We need to add the null character '\0' at the end of the array by
ourself whereas, it is appended internally by the compiler in the
case of the character array.
• The string literal cannot be reassigned to another set of
characters whereas, we can reassign the characters of the array.
String Example in C
Let's see a simple example where a string is declared and being
printed. The '%s' is used as a format specifier for the string in c
language.
1.#include<stdio.h>
2.int main(){
3. char ch1[12]={‘P', ’r', ’o', ’g', ’r', ’a', ’m', ’m', ’i', ’n', ’g’, '\0'};
4. char ch2[12]=" Programming";
5.
6. printf("Char Array Value is: %s\n", ch1);
7. printf("String Literal Value is: %s\n", ch2);
8. return 0; Output
9.}
Char Array Value is: Programming
String Literal Value is: Programming
Loop Through a String
Let's see a simple example where a string is declared and being
printed. The '%s' is used as a format specifier for the string in c
language.
1.#include<stdio.h>
2.int main(){
3. char ch2[12]="Programming";
4. for(int i =0; i<11;i++){
5. printf("%c", ch2[i]);
6.}
7. return 0;
8.}
Traversing String
Traversing the string is one of the most important aspects in any of
the programming languages. We may need to manipulate a very
large text which can be done by traversing the text. Traversing
string is somewhat different from the traversing an integer array. We
need to know the length of the array to traverse an integer array,
whereas we may use the null character in the case of string to
identify the end the string and terminate the loop.
It is clear from the output that, the above code will not work for
space separated strings. To make this code working for the space
separated strings, the minor changed required in the scanf function,
i.e., instead of writing scanf("%s",s), we must write: scanf("%[^\
n]s",s) which instructs the compiler to store the string s while the
new line (\n) is encountered.
To store the space-separated strings
1.#include<stdio.h>
2.void main ()
3.{
4. char s[20];
5. printf("Enter the string?");
6. scanf("%[^\n]s",s);
7. printf("You entered %s",s);
8.}
It is clear from the output that, the above code will not work for
space separated strings. To make this code working for the space
separated strings, the minor changed required in the scanf function,
i.e., instead of writing scanf("%s",s), we must write: scanf("%[^\
n]s",s) which instructs the compiler to store the string s while the
new line (\n) is encountered.
To store the space-separated strings
C gets() function
The gets() function enables the user to enter some characters
followed by the enter key. All the characters entered by the user get
stored in a character array. The null character is added to the array
to make it a string. The gets() allows the user to enter the space-
separated strings. It returns the string entered by the user.