Professional Documents
Culture Documents
Chapter 7.2
Chapter 7.2
Chapter 7.2
Introduction
String is a sequence of characters that is treated as a single data item and terminated by null
character '\0'. Remember that C language does not support strings as a data type. A string is
actually one-dimensional array of characters in C language. These are often used to create
meaningful and readable programs.
For example: The string "hello world" contains 12 characters including '\0' character which is
automatically added by the compiler at the end of the string. The operations performed on
character strings are:
Reading and writing strings
Combining strings together
Copying one string to another
Comparing strings for equality
Extracting a portion of a string
Declaring and Initializing a string variables
The general form of a string is:
char string_name[size];
There are different ways to initialize a character array variable.
char name[13] = "BceClasses"; // valid character array initialization
char name[10] = {'C', 'h', 'a', 'p', 't', 'e', 'r', 's','\0'}; // valid initialization
Remember that when you initialize a character array by listing all of its characters separately
then you must supply the '\0' character explicitly.
Some examples of illegal initialization of character array are,
char ch[2] = "bce"; // Illegal
char str[3];
str = "bce"; // Illegal
C also permits to initialize the character array without specifying the numbers of elements as:
char name[] = "ACEM Kupondole";
char name[] = {'A', 'C', 'E', 'M', ' ', 'K', 'u', 'p', 'o', 'n', 'd', 'o', 'l', 'e', '\0'};
strcat() function
Syntax: strcat(string1, string2);
strcat("BCE", "CLASSES");
strcat() function will add the string "BCE" to "CLASSES" i.e., it will output BCECLASSES.
Result is stored in first parameter.
Q. WAP to concatenate two string using user defined function concatenate(). [Don't use
string library function strcat().
#include<stdio.h>
#include<conio.h>
void concatenate(char [], char []);
int main()
{
char s1[50], s2[50];
printf("\nEnter string1 : ");
gets(s1);
printf("\nEnter string2 : ");
gets(s2);
concatenate(s1, s2);
printf("\nConcatenated string is %s", s1);
getch();
return 0;
}
strlen() function
Syntax : len = strlen(string); where l is the integer variable which receives the value of the
length of the string.
strlen() function will return the length of the string passed to it.
int j;
j = strlen("BCE CLASSESS");
printf("%d",j);
Output : 12
Without using strlen() function
#include<stdio.h>
int stringlength(char []);
int main()
{
char str[50] = "Bce Classes";
int length;
length = stringlength(str);
printf("Length of string is %d.", i);
return 0;
getch();
}
int stringlength(char str[])
{
int len;
while(str[len]!='\0')
{
len++;
}
return len;
}
Output : Length of string is 11.
Output : -1
Output:
Enter first string : aaaa
Enter second string : aaaaa
Difference is -97, so strings are unequal.
Enter 3 strings
Engineering
College
Civil
In dictionary order
Civil
College
Engineering
Q. WAP to read a sentence and reprint by replacing all the occurrence of the substring
"the" by "***".
#include <stdio.h>
#include <string.h>
#include<conio.h>
int main()
{
char str[100],word[10],rpwrd[10],str[10][10];
int i=0,j=0,k=0,w,p;
printf("PLEASE WRITE ANY TEXT.\n");
printf("GIVE ONLY ONE SPACE AFTER EVERY WORD\n");
gets(text);
printf("\nENTER WHICH WORD IS TO BE REPLACED\n");
scanf("%s",word);
printf("\nENTER BY WHICH WORD THE %s IS TO BE REPLACED\n",word);
scanf("%s",rpwrd);
p=strlen(text);
P u n e e t S h a r m a \0
A m a n \0
S h y a m \0
Here sorting of array of string using pointer can be done by swapping pointers. But sorting of
array of string without using pointer deals with copying the string from one string to another
string.
By using pointer, we can eliminate
Complicated storage management
High overheads of moving lines
Q. Sorting of strings using pointer
#include<stdio.h>
void namesort(char **);
int main()
{
char *names[5];
int i;
printf("Enter 5 different words\n");
for(i=0;i<len;i++)
{
for(j=0;j<len-1;j++)
{
if(str[j]>str[j+1])
{
temp=str[j];
str[j]=str[j+1];
str[j+1]=temp;
}
}
}
printf("%s\n",str);
getch();
}