Class notes (Programming in C and Data Structure) (1 st Semester)(Section-A) (Date: 21-11-2024)
Gandhi Institute for Education and Technology, Baniatangi
String
Array of character is called a string. It is always terminated by the NULLcharacter. String is a
one dimensional array of character.
We can initialize the string as char
name[]={‘j’,’o’,’h’,’n’,’\o’};
Here each character occupies 1 byte of memory and last character is always NULLcharacter. Where ’\o’
and 0 (zero) are not same, where ASCII value of ‘\o’ is 0 and ASCII value of 0 is 48. Array elements of
character array are also stored in contiguous memory allocation.
From the above we can represent as;
J O h N ‘\o’
The terminating NULL is important because it is only the way that thefunction that work
with string can know, where string end.
String can also be initialized as;char
name[]=”John”;
Here the NULL character is not necessary and the compiler will assume itautomatically.
String constant (string literal)
A string constant is a set of character that enclosed within the double quotesand is also called a
literal. Whenever a string constant is written anywhere in a program it is stored somewhere in a memory
as an array of characters terminated by a NULL character (‘\o’).
Example – “m”
“Tajmahal”
“My age is %d and height is %f\n”
The string constant itself becomes a pointer to the first character in array.Example-char
crr[20]=”Taj mahal”;
1000 1001 1002 1003 1004 1005 1006 1007 100 1009
T a j M A H a l \o
1
Class notes (Programming in C and Data Structure) (1 st Semester)(Section-A) (Date: 21-11-2024)
Gandhi Institute for Education and Technology, Baniatangi
String library function
There are several string library functions used to manipulate string and the prototypes for these
functions are in header file “string.h”. Several string functions are
strlen()
This function return the length of the string. i.e. the number of characters in thestring excluding the
terminating NULL character.
It accepts a single argument which is pointer to the first character of the string.
For example-
strlen(“suresh”);
It return the value 6.
In array version to calculate legnth:-int str(char
str[])
int i=0;
while(str[i]!=’\0’)
i++;
}
return i;
Example:-
#include<stdio.h>
#include<string.h> void
main()
char str[50]; print(”Enter a
string:”);
2
Class notes (Programming in C and Data Structure) (1 st Semester)(Section-A) (Date: 21-11-2024)
Gandhi Institute for Education and Technology, Baniatangi
gets(str);
printf(“Length of the string is %d\n”,strlen(str));
Output:
Enter a string: C in DepthLength
of the string is 8
strcmp()
This function is used to compare two strings. If the two string match, strcmp()return a value 0 otherwise
it return a non-zero value. It compare the strings character by character and the comparison stops when
the end of the string is reached or the corresponding characters in the two string are not same.
strcmp(s1,s2)
return a value:
<0 when s1<s2
=0 when s1=s2
>0 when s1>s2
The exact value returned in case of dissimilar strings is not defined. We only knowthat if s1<s2 then a
negative value will be returned and if s1>s2 then a positive value will be returned.
For example:
/*String comparison… ............................................ */
#include<stdio.h>
#include<string.h>void
main()
{
3
Class notes (Programming in C and Data Structure) (1 st Semester)(Section-A) (Date: 21-11-2024)
Gandhi Institute for Education and Technology, Baniatangi
char str1[10],str2[10]; printf(“Enter
} two strings:”);gets(str1);
else gets(str2); if(strcmp(str1,str2)==0)
{
printf(“String are same\n”);
}
printf(“String are not same\n”);
4
Class notes (Programming in C and Data Structure) (1 st Semester)(Section-A) (Date: 21-11-2024)
Gandhi Institute for Education and Technology, Baniatangi
strcpy()
This function is used to copying one string to another string. The function strcpy(str1,str2) copies str2 to
str1 including the NULL character. Here str2 is thesource string and str1 is the destination string.
The old content of the destination string str1 are lost. The function returns a pointerto destination string
str1.
Example:-
#include<stdio.h>
#include<string.h>void
main()
char str1[10],str2[10];
printf(“Enter a string:”);
scanf(“%s”,str2);
strcpy(str1,str2);
printf(“First string:%s\t\tSecond string:%s\n”,str1,str2);
strcpy(str,”Delhi”);
strcpy(str2,”Bangalore”);
printf(“First string :%s\t\tSecond string:%s”,str1,str2);
strcat()
This function is used to append a copy of a string at the end of the other string. If the first string is
“”Purva” and second string is “Belmont” then after using this function the string becomes
“PusvaBelmont”. The NULL character from str1 is moved and str2 is added at the end of str1. The 2nd
string str2 remains unaffected. A pointer to the first string str1 is returned by the function.
Example:-
#include<stdio.h>
#include<string.h>void
5
Class notes (Programming in C and Data Structure) (1 st Semester)(Section-A) (Date: 21-11-2024)
Gandhi Institute for Education and Technology, Baniatangi
main()
char str1[20],str[20]; printf(“Enter
two strings:”);gets(str1);
gets(str2);
strcat(str1,str2);
printf(“First string:%s\t second string:%s\n”,str1,str2);strcat(str1,”-
one”);
printf(“Now first string is %s\n”,str1);
Output
Enter two strings: dataBase
First string: database second string: database
` Now first string is: database-one
Write a program in C to join/concatenate two strings without using any library function.
#include <stdio.h> // Integrate the Standard I/O library
int main() { // Commence main function
char str1[100], str2[100]; // Arrays to store the two strings
int i = 0, j = 0; // Counters for string traversal
// Obtain the strings from the user
printf("Enter the first string: ");
scanf("%s", str1);
printf("Enter the second string: ");
scanf("%s", str2);
// Traverse to the end of the first string
while (str1[i] != '\0') {
i++;
}
// Copy characters of the second string to the end of the first string
while (str2[j] != '\0') {
str1[i] = str2[j];
6
Class notes (Programming in C and Data Structure) (1 st Semester)(Section-A) (Date: 21-11-2024)
Gandhi Institute for Education and Technology, Baniatangi
i++;
j++;
}
str1[i] = '\0'; // Terminate the concatenated string with a null character
// Display the concatenated string
printf("Concatenated String: %s\n", str1);
return 0; // Terminate the program gracefully
Write a program in C to reverse a string without using any library function.
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main(){
char string[20],temp;
int i,length;
printf("Enter String : ");
scanf("%s",string);
length=strlen(string)-1;
for(i=0;i<strlen(string)/2;i++){
temp=string[i];
string[i]=string[length];
string[length--]=temp;
}
printf(" Reverse string :%s",string);
getch();
}
Write a program in C to check a string is palindrome or not without using any library function.
#include <stdio.h>
#include <string.h>
void main()
{
char string[25], reverse_string[25] = {'\0'};
int i, length = 0, flag = 0;
printf("Enter a string \n");
gets(string);
/* keep going through each character of the string till its end */
for (i = 0; string[i] != '\0'; i++)
{
length++;
}
printf("The length of the string '%s' = %d\n", string, length);
7
Class notes (Programming in C and Data Structure) (1 st Semester)(Section-A) (Date: 21-11-2024)
Gandhi Institute for Education and Technology, Baniatangi
for (i = length - 1; i >= 0 ; i--)
{
reverse_string[length - i - 1] = string[i];
}
/* Check if the string is a Palindrome */
for (flag = 1, i = 0; i < length ; i++)
{
if (reverse_string[i] != string[i])
flag = 0;
}
if (flag == 1)
printf ("%s is a palindrome \n", string);
else
printf("%s is not a palindrome \n", string);
}