Strings in C
Definition : It is a collection of characters. Character arrays are used to handle strings.
Declaration:
char string_name[length+1];
Note: Here the purpose of +1 is null character ( \0 ) to be maintained at end of the string. It indicates end of the string.
Any string operation performed up to null
char ( \0 ), which is end of the string .
To accommodate null char one extra byte
required.
Eg:
char str[10]
à it allows only 9
chars.
Memory Map of a String:
str[0] str[1] str[2] str[3] str[4] str[5] str[6] str[7] str[8] str[9]
|
|
|
|
|
|
|
|
|
|
Some String Operations are as follows:
1. Reading of a String
2. Displaying and writing of string
3. Length of a String
4. Copying strings
5. Comparison of strings
6. Concatenation of string
7. Reverse of a string.
Assignment: char str[10];
str[0] str[1] str[2] str[3] str[4] str[5] str[6] str[7] str[8] str[9]
H |
E |
L |
L |
O |
\0 |
|
|
|
|
str[0]=’H’;
str[1]=’E’;
str[2]=’L’;
str[3]=’L’;
str[4]=’O’;
str[5]=’\0’;
str=”hello” ; Ã invalid
Multiple characters cannot be assign to
strings.
In this scenario better to use strcpy() function.
Eg:
strcpy(str,”HELLO”);
Initialization:
char str[10]=”hello”;
char str[]=”hello”;
char str[6]=”hello”;
char str[5]=”hello”; Ã invalid, since one extra byte
required to accommodate null char ( ‘\0’) into string.
This statement compile but it raises
memory exception at run time.
Reading a string from key board:
scanf(“%s”,str); Ã
to read strings & is not require
since, string name itself is an address of the sting.
Eg :
input :
hello world Ã
scanf can read upto only space or tab or new line character ( enter key ).
Here
value of str is “hello”
Displaying string:
printf(“%s”,str);
Program to read and display a string.
#include<stdio.h>
#include<conio.h>
main()
{
char str1[30],str2[30];
printf("\n Enter two
strings");
scanf("%s %s",str1,str2);
printf("%s %s",str1,str2);
getch();
}
Another way of reading a line is by using gets() Ã is function to read a line.
gets(str) Ã
it will read input data up to enter key. It will read data including \n,
but will not store into string.
If input data is more than the size of the string, then data overflow, hence it raises memory exception. So It is not recommended to use .
And its alternative is : fgets() Ã is an alternative to gets() in reading a line.
char str[10];
int n;
n =sizeof(str);
fgets(str,n,stdin);
This function will read input data up to
enter key or ( n-1) chars, which ever comes first.
puts()Ã
to display the string.
char str[10]=”hello”;
puts(str); (or) puts(“hello”); (or)
puts(“\n hello \n world”):
fputs()
à also to display
the string.
Eg:
fputs(str, stdout);
this function is to insert \n at the data
and write to output buffer. Hence cursor moves to next line.
Input
function |
Output
function |
scanf() |
printf() |
getchar() |
putchar() |
gets() |
puts() |
fgets() |
fputs() |
Program to display following out put as follows:
h
he
hel
hell
hello
#include<stdio.h>
main()
{
char str[50];
int n,d;
printf("\nEnter string1");
scanf("%s",str);
n=strlen(str);
for(d=1;d<=n;d++)
{
printf("\n%*.*s",n,d,str);
}
getch();
}
1 Comments
Excellent blog.very easy to learn
ReplyDelete