11.Working With Strings
11.Working With Strings
In C language the group of characters, digits, and symbols enclosed within quotation
marks are called as strings. The string is always declared as character arrays. In other
words character arrays are called Strings. Every string is terminated with ‘\0’ (NULL)
character. The NULL character is a byte with all bits at logic zero. Hence, its decimal
value is zero.
For Example,
char name[]={‘I’, ‘N’, ‘D’, ‘I’, ‘A’, ‘\0’};
Each character of the string occupies 1 byte of memory. The last character
is always ‘\0’. It is not compulsory to write ‘\0’ in string. The compiler automatically puts
‘\0’ at the end of the character array or string. The characters of string are stored in
contigous memory locations.
I N D I A \0
char name[]=”india”;
The C compiler inserts the NULL (\0) character automatically at the end of the
string. So initialization of NULL character is not essential.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char arr1[9]={‘w’,’e’,’l’,’ ‘,’c’,’o’,’m’,’e’,’\0’};
char arr2[9]=”welcome”;
char arr3[9]={{‘w’},{‘e’},{‘l’},{‘ ‘},{‘c’},{‘o’},{‘m’},{‘e’}};
clrscr();
printf(“\narray1=%s”,arr1);
printf(“\narray2=%s”,arr2);
printf(“\narray3=%s”,arr3);
getch();
}
Use the while loop and print out the elements of the character array.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
char text[]=”HAVE A NICE DAY”;
int i=0;
clrscr();
while(i<=15)
{
printf(“%c”,text[i]);
i++;
}
getch();
}
WAP to print out the elements of the character array using while loop
#include<stdio.h>
#include<conio.h>
void main()
{
char text[]=”have a nice day”;
int i=0;
clrscr();
while(text[i]!=’\0’;
{
printf(“%c”,text[i]);
i++;
}
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char text[10];
int len;
clrscr();
printf(“\nenter the string :”);
gets(text);
len=strlen(text);
printf(“length of string=%d”,len);
getch();
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[10];
char str2[10];
clrscr();
printf(“\nenter the string :”);
gets(str1);
strcpy(str2,str1);
printf(“str2=%s”,str2);
getch();
}
Without function
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[10];
char str2[10];
int I;
clrscr();
printf(“\n Enter the string :”);
gets(str1);
for(i=0;str1[i]!=’\0’;i++)
{
Str2[i]=str1[i];
}
printf(“str2=%s”,str2);
getch();
}
strcmp() -> Compares two strings. (Function doesn’t discriminates between small &
capital letters.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[15];
char str2[15];
int n;
clrscr();
printf(“\n Enter First string :”);
gets(str1);
printf(“\n Enter Second string :”);
gets(str2);
if(strcmp(str1,str2)==0)
printf(“\n String are equal”);
else
printf(“\n String are not equal”);
getch();
}
stricmp()-> It compares the two strings. The characters of the strings may be in lower
case or upper case the function doesn’t discriminates between them. i.e This function
compares two strings without case. If the strings are same it returns to zero otherwise
non-zero value.
strstr()-> This function finds second string in the first string. It returns the pointer
location from where the second string starts in the first string. In case the first occurrence
in the string is not observed, the function returns a NULL character.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[15];
char str2[15];
char *chp;
clrscr();
printf(“\n Enter First string :”);
gets(str1);
printf(“\n Enter Second string :”);
gets(str2);
chp=strstr(line1,line2);
if(chp)
printf(“’%s’ String is present in given string”,str2);
else
printf(“’%s’ String is not present in given string”,str2);
getch();
}
strcat() -> This function appends the target string to the source string. Concatenation of
two strings can be done using this function.
strcat(str1,str2);
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[15];
char str2[15];
clrscr();
printf(“\n Enter First string :”);
gets(str1);
strcat(str2,str1);
printf(“\n Str2=%s”,str2);
getch();
}
Application of Strings
WAP to count a character that appears in a given text for number of times. Use the
while loop.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[15];
char find;
int i=0,count=0;
clrscr();
printf(“\n Enter First string :”);
gets(str1);
printf(“Type a character to count :”);
find=getche();
while(str1[i]!=’\0’)
{
If(str[i]==find)
count++;
i++;
}
printf(“\n character (%c) found in given string=%d times.”,find,count);
getch();
}
WAP to count ‘m’ characters that appears in a given string without using any
function. Use the for loop.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[15];
int i=0,count=0;
clrscr();
for(i=0;i<25;i++)
{
scanf(“%c”,&str1[i]);
printf(“%c”,str1[i]);
if(st1[i]==’\n’)
break;
else
if(str1[i]==’m’)
++count;
}
WAP to count the following characters that appears in a string without using any
functions.
1. ‘m’
2. ‘r’
3. ‘o’
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[15]=”Programming is good habit”;
int i,m=0,o=0,r=0;
clrscr();
printf(“\n Enter First string :”);
gets(str1);
for(i=0;i<25;i++)
{
If(str1[i]==’m’)
++m;
If(str1[i]==’r’)
++r;
If(str1[i]==’o’)
++o;
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<process.h>
void main()
{
char str1[15];
int i=0,j,test;
clrscr();
printf(“\n Enter string :”);
scanf(“%s”,str);
j=strlen(str1)-1;
while(i<=j)
{
if(str1[i]==str1[j])
test=1;
else
{
test=0;
break;
}
i++;
j--;
}
if(test==1)
printf(“\n word is palindrome”);
else
printf(“\n word is not palindrome”);
getch();
}
WAP to find number of words in a given statement. Exclude spaces between them.
#include<stdio.h>
#include<conio.h>
void main()
{
char str1[35];
int count=0,i=0;
clrscr();
printf(“\n Enter the string :”);
gets(str1);
while(text[i++]!=’\0’)
{
If(text[i]==32 || text[i]==’\0’)
count++;
}
printf (“\n The no. of words in line=%d”, count);
getch();
}