Strings In C language
String:-
String is group of alphabets,digits and symobls. In C language, a string represented by char array.
e.g. char S[20];
In this example S is a string of 20 characters but it can hold a string of only 19 characters, because the last character of a string is always NULL.
NULL :-
It is a character used to repersent end of String.
It has three repersentactions:
- NULL (need stdio.h)
- ’’
- 0 </code>
Output with String:-
1. Using printf() function:
printf will display the entire String using %s
e.g.
char a[20] = "Vipin Yadav";
printf("The String is %s",a);
// The String is Vipin Yadav
printf("The String is %.4s",a);
// The String is Vipi ( Here .4 will allow us to print only 4 letter of string only)
printf("The String is %10.4s",a);
// The String is ------Vipi
// Here 10.4 mean print 10 letter but use 4 letter from string here it take 4 letter and
// use 6 ' ' but i use '-' instead of ' ' to show you
2. Using puts() function:
Input with String:-
1. Using scanf() function:
scanf() with %s :- It can read one word of a string or a one word string.
e.g.
char a[20];
printf("Enter a string who contain lases then 20 words : ");
scanf("%s",a);
// NOTE : WE DO NOT USE '&' DURING A STRING INPUT.
puts(a);
scanf() with %[^n] :- It can read a multi word string .
char a[20];
printf("Enter a string who contain lases then 20 words : ");
scanf("%s",a);
// NOTE : WE DO NOT USE '&' DURING A STRING INPUT.
puts(a);
2. Using gets() function :-
gets() function can read multiple word once It work under stdio.h heder file
e.g.
char a[20];
printf("Enter a string who contain lases then 20 words : ");
gets(a);
puts(a);
In next post we will read about Inbuilt function and user-define of strings.
