String is a set of characters that are enclosed in double quotes. In the C programming language, strings are created using one dimension array of character datatype. Every string in C programming language is enclosed within double quotes and it is terminated with NULL (\0) character. Whenever c compiler encounters a string value it automatically appends a NULL character (\0) at the end. The formal definition of string is as follows...
String is a set of characters enclosed in double quotation marks. In C programming, the string is a character array of single dimension.
In C programming language, there are two methods to create strings and they are as follows...
- Using one dimensional array of character datatype ( static memory allocation )
- Using a pointer array of character datatype ( dynamic memory allocation )
In C, strings are created as a one-dimensional array of character datatype. We can use both static and dynamic memory allocation. When we create a string, the size of the array must be one more than the actual number of characters to be stored. That extra memory block is used to store string termination character NULL (\0). The following declaration stores a string of size 5 characters.
char str[6] ;
The following declaration creates a string variable of a specific size at the time of program execution.
char *str = (char *) malloc(15) ;
String value is assigned using the following two methods...
- At the time of declaration (initialization)
- After declaration
{
char str1[6] = "Hello";
char str2[] = "Hello!";
char name1[] = {'s','m','a','r','t'};
char name2[6] = {'s','m','a','r','t'};
char title[20];
*title = "bca class";
return 0;
}
Traversing the string is one of the most important aspects in any of the programming languages. 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.
Hence, there are two ways to traverse a string.
- By using the length of string
- By using the null character.
Let's discuss each one of them.
Using the length of string
Let's see an example of counting the number of vowels in a string.
#include<stdio.h>
void main ()
{
char s[11] = "prasanjeet";
int i = 0;
int count = 0;
while(i<11)
{
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{
count ++;
}
i++;
}
printf("The number of vowels %d",count);
}
Output
Let's see the same example of counting the number of vowels by using the null character.
#include<stdio.h>
void main ()
{
char s[11] = "c programming";
int i = 0;
int count = 0;
while(s[i] != NULL)
{
if(s[i]=='a' || s[i] == 'e' || s[i] == 'i' || s[i] == 'u' || s[i] == 'o')
{
count ++;
}
i++;
}
printf("The number of vowels %d",count);
}
We can read a string value from the user during the program execution. We use the following two methods...
- Using scanf() method - reads single word
- Using gets() method - reads a line of text
Using scanf() method we can read only one word of string. We use %s to represent string in scanf() and printf() methods.
#include<stdio.h>
#include<conio.h>
int main(){
char name[50];
printf("Please enter your name : ");
scanf("%s", name);
printf("Hello! %s , welcome to BCA class !!", name);
return 0;
}
When we want to read multiple words or a line of text, we use a pre-defined method gets(). The gets() method terminates the reading of text with Enter character.
#include<stdio.h> #include<conio.h> int main(){ char name[50]; printf("Please enter your name : "); gets(name)|; printf("Hello! %s , welcome to BCA class !!", name); return 0; }
C puts() function
The puts() function is very much similar to printf() function. The puts() function is used to print the string on the console which is previously read by using gets() or scanf() function. The puts() function returns an integer value representing the number of characters being printed on the console.
#include<stdio.h>
#include <string.h>
int main(){
char name[50];
printf("Enter your name: ");
gets(name); //reads string from user
printf("Your name is: ");
puts(name); //displays string
return 0;
}
No comments:
Post a Comment