0% found this document useful (0 votes)
5 views

11.Working With Strings

The document provides an overview of strings in the C programming language, explaining their definition, declaration, and initialization as character arrays. It also covers various standard string functions such as strlen, strcpy, strcmp, and strcat, along with examples of their usage. Additionally, it includes several programming exercises to demonstrate string manipulation, counting characters, and checking for palindromes.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

11.Working With Strings

The document provides an overview of strings in the C programming language, explaining their definition, declaration, and initialization as character arrays. It also covers various standard string functions such as strlen, strcpy, strcmp, and strcat, along with examples of their usage. Additionally, it includes several programming exercises to demonstrate string manipulation, counting characters, and checking for palindromes.

Uploaded by

Abadullah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

Working with Strings & Standard Functions

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.

Character arrays can be initialized as follows

a) char name[6]={‘s’,’a’,’n’,’j’,’a’,’y’}; garbage value


b) char name[7]={ ‘s’,’a’,’n’,’j’,’a’,’y’}; sanjay

WAP to print “WELCOME” by using different formats of initialiazation of array.

#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++;
}
}

String Standard Functions

1) strlen()-> The function counts the number of characters in a given string.

WAP to count the number of characters in a given string

#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();
}

WAP to count the number of characters without strlen


#include<stdio.h>
#include<conio.h>
void main()
{
char text[10];
int i,len=0;
clrscr();
printf(“\n enter the string :”);
gets(text);
for(i=0;text[i]!=’\0’;i++)
{
len++;
}
printf(“\nlength=%d”,len);
getch();
}

strcpy() -> Copies a string from source to destination.

#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;
}

printf(“\n character ‘m’ found in given string=%d times.”,count);


getch();
}

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;

printf(“\n Character ‘m’ found in text=%d times\n”,m);


printf(“\n Character ‘r’ found in text=%d times\n”,r);
printf(“\n Character ‘o’ found in text=%d times\n”,o);
getche();
}

WAP to know whether the entered character string is palindrome or not.

#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();
}

You might also like