Strings:
char data type:
It is a primary data type used to store a single character
constant
It takes 1 byte of memory.
Single character constant must be placed within single
quotations.
When a character constant is assigned then its binary equal (ASCII)
is stored in the memory.
%c is used to represent character type in I/O statements.
Example:
#include<stdio.h>
void main()
{
char ch='a';
clrscr();
printf("%c",ch);
getch();
}
Output:
a
NULL character:
In C language '\0' is considered as NULL character.
The ASCII value of '\0' is 0
Example:
#include<stdio.h>
void main()
{
char ch='\0';
clrscr();
printf("%d",ch);
getch();
}
Output:
0
Character array:
Set of character constants stored in contiguous memory
allocations is called a character array.
When a character array is declared then the C compiler
allocates the memory allocation of elements and stores the address of base
element in a pointer with the name of character array.
Now the pointer can refer every element using either pointer
arithmetics *(x+i) or using index x[i].
Example:
#include<stdio.h>
void main()
{
char x[]={'r','o','c','k','i','n','g'};
int i;
clrscr();
for(i=0;i<7;i++)
printf("%c",x[i]);
getch();
}
Output:
rocking
String:
Any word placed within double quotations is called a string.
In C language there is no data type to handle strings
We need to use a character array to store a string as
individual character constants.
When a string is assigned to character array then individual
characters are stored in a given sequence and a terminating character '\0' is
automatically added.
The size of array is more than the size of string.
Example:
#include<stdio.h>
void main()
{
char
x[]="rocking";
int i;
clrscr();
for(i=0;x[i]!='\0';i++)
printf("%c",x[i]);
getch();
}
Output:
rocking
Example explained:
Here the terminating character '\0' is automatically added
at the end of string.
The condition (x[i]!='\0') helps to traverse the string without
knowing the length.
Accepting a string from the keyboard:
As we know a string is a combination of characters.
In order to accept a string from the keyboard we need to accept
character by character, which is difficult to code.
C language has an automated way of accepting string through
scanf() function.
If we specify the address of character array (name) then
scanf() accepts character by character and stores in to array.
A terminating character '\0' is automatically added.
Representation character %s is used to represent string in
I/O functions.
Example:
Printing the string:
Because a string is a combination of characters, it is
difficult to code.
The printf() automatically prints the string if the address
(name) of character array is given.
Example:
printf("%s",x);
Specification1:
Accept a string from the keyboard and print by using a loop.
Program:
#include<stdio.h>
void main()
{
char x[50];
int i;
clrscr();
printf("Enter
any string:");
scanf("%s",x);
printf("The
given string:");
for(i=0;x[i]!='\0';i++)
printf("%c",x[i]);
getch();
}
Execution:
Enter any string: nice
The given string nice
Program explained:
The scanf() accepted the string and automatically stored in
character array character by character.
Terminating character '\0' is automatically added.
Character by character is printer until '\0' using a loop.
Specification2:
Accept any string from the keyboard and print the same
without using a loop.
Program:
#include<stdio.h>
void main()
{
char x[50];
int i;
clrscr();
printf("Enter
any string:");
scanf("%s",x);
printf("The
given string:");
printf("%s",x);
getch();
}
Execution:
Enter any string: nice
The given string nice
Program explained:
Here scanf() and printf() are used to accept and print the
strings.
Specification3:
Accept any string from the keyboard and print using the
function display()
Program:
#include<stdio.h>
void display(char*);
void main()
{
char x[50];
clrscr();
printf("Enter
any string:");
scanf("%s",x);
display(x);
getch();
}
void display(char *p)
{
/* int i;
for(i=0;p[i]!='\0';i++)
printf("%c",p[i]); */
printf("%s",p);
}
Execution:
Enter any string:
Program explained:
Here while sending a string as an argument we specified the
name of string (x) as an argument which is the address of array.
A character pointer (p) is defined to store the address of character
array.
Now both "x" and "p" refers the same
array from different locations.
Specification4:
Accept any string from the keyboard and print its length
using the function length()
Program:
#include<stdio.h>
int length(char*);
void main()
{
char x[50];
int l;
clrscr();
printf("Enter
any string:");
scanf("%s",x);
l=length(x);
printf("The
length of string %d",l);
getch();
}
int length(char *p)
{
int len,i;
for(len=0,i=0;p[i]!='\0';i++)
len++;
return len;
}
Execution:
Enter any string:
The length of string 7
Program explained:
Here we count how many times the loop iterates which is the
length of string.
0 comments:
Post a Comment