PR 6
PR 6
06
THEORY:
In C programming, array of character are called strings. A string is terminated by null
character ‘/0’. For example,
Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null
character at the end of string.
DECLARATION OF STRINGS
Strings are declared in C in similar manner as arrays. Only difference is that, strings are of
char type.
char s[5];
char *p
INITIALIZATION OF STRINGS
Page 1 of 5
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
char c[]={'a','b','c','d','\0'};
OR;
char c[5]={'a','b','c','d','\0'};
char *c="abcd";
String variable c can only take a word. It is beacause when white space is encountered, the
scanf() function terminates.
EXAMPLE:
#include <stdio.h>
void main(){
char name[20];
scanf("%s",name);
OUTPUT:
Here, program will ignore Ritchie because, scanf() function takes only string before the
white space.
void main(){
char name[30];
printf("Name: ");
getch();
OUTPUT
Enter name: Tom Hanks
Name: Tom Hanks
STRING FUNCTIONS:
S.N. Function & Purpose
1 strcpy(s1, s2);
2 strcat(s1, s2);
Page 3 of 5
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
4 strcmp(s1, s2);
5 strchr(s1, ch);
6 strstr(s1, s2);
EXAMPLE:
#include <stdio.h>
#include <string.h>
void main () {
char str3[12];
int len ;
strcpy(str3, str1);
Page 4 of 5
strcat( str1, str2);
len = strlen(str1);
getch();
OUTPUT:
strlen(str1) : 10
EXERCISE:
1. Write a program to count the number of ‘A’ in the string entered by user.
2. Write a programto reverses the string.
3. Write a program to check whether entered string is palindrome or not.
4. Write a program to convert lower case string to upper case.
5. Write a program to check whether entered two strings are same or not.
Page 5 of 5