Wa0018.
Wa0018.
Unit-03
• In memory, a string ends with a null character ‘\0’ and it occupies 1 byte of memory.
• C compiler automatically adds a NULL character '\0' to the character array created.
2
Declaration Of Strings
• C language does not directly support string as a data type. Hence, to display a String in C, need to make
use of a character array.
• Syntax:
• Example:
char EmployeeName[20];
Here 20 implies that the maximum number of characters can be 19 and one position is reserved for ‘\0’
Since a character occupies one byte, the above array occupies 20 bytes (19 bytes for the employee name and
one byte for ‘\0’)
4
Static Initialization of Strings
5
Contd…
• char acItemCategory[3]=“Books”;
• Here the size specified is 3. But the number of characters in the string is 5. This is
invalid.
• char acItemCategory[ ]={'s','t','a','t','i','o','n','a','r','y','\0'};
• Here the character constants are supplied to initialize the string.
6
Reading and Printing Strings - Example
7
String Handling Functions
• The following are the string built-in functions that are supported by C
strlen() strcpy() strcat() strcmp()
• These functions are defined in string.h header file.
• All these functions take either a character pointer or a character array
as an argument.
8
strlen() Function
• strlen() function is used to count the number of characters in the
string. Counts all the characters excluding the null character ‘\0’
• Syntax:
strlen (str1);
• The strlen() function is defined in <string.h> header file.
9
Contd…
• Example:
strlen(“Programming Fundamentals”);
/*returns 24*/
strlen(Category);
• returns the number of characters in the character array ‘Category’
10
Example: C strlen() function
#include <stdio.h>
#include <string.h>
int main() {
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
char c[20]; Output:
printf("Enter string: "); Enter string C: PCU
gets(c); //scanf(“%s”,c); Length of string a = 7
printf("Length of string a = %d \n",strlen(a)); Length of string b = 7
Length of string c = 3
//calculates the length of string before null character.
11
strcpy() Function
12
Contd…
• The previous contents of Dest_String, if any, will be over written
• Example:
char CourseName[40];
strcpy(CourseName , “C Programming”);
• Now CourseName will get the value “C Programming”
13
Example: C strcpy() #include <stdio.h>
Function #include <string.h>
int main()
{
char str1[10]= "awesome";
char str2[10];
char str3[10];
strcpy(str2, str1); //function call Output:
strcpy(str3, "well");
awesome
puts(str2);
well
puts(str3);
return 0;
}
14
strcat() Function
• strcat() function is used to concatenate (Combine) two strings
• Syntax:
strcat( Dest_String_Variable , Source_String ) ;
• In this, the Destination should be a variable and Source_String can either be a string
constant or a variable.
• The contents of Dest_String is concatenated with Source_String contents and the
resultant string is stored into Dest_String variable.
15
Contd…
• Example:
char Course [50] = “The course is “;
strcat(Course,”Oracle 8i”);
16
Example: C strcat() function
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = “Welcome to", str2[] = “PCU";
//concatenates str1 and str2 and resultant string is stored in str1. Output:
strcat(str1,str2);
puts(str1); Welcome to PCU
puts(str2); PCU
return 0;
}
17
strcmp() Function
• strcmp() function is used to compare two strings. This is case
sensitive.
• Syntax:
int strcmp( String1 , String2 )
• Here both String1 and String2 can either be a variable or a string
constant
• strcmp() function returns an integer value.
18
Contd…
• If both the strings are equal it returns zero.
• If the first string is alphabetically greater than the second string then it returns a positive
value.
• If the first string is alphabetically less than the second string then it returns a negative
value.
• Example:
strcmp(“My Work”, “My Job”) returns a positive value.
19
Contd…
Return Value from
Remarks
strcmp()
if strings are equal
0
string 1(first n characters) = string 2(first n
(Zero)
characters)
if the first non-matching character in str1 is
>0 greater (in ASCII) than that of str2.
(Positive) string 1(first n characters) > string 2(first n
characters)
if the first non-matching character in str1 is
<0 lower (in ASCII) than that of str2.
(Negative) string 1(first n characters) < string 2(first n
characters)
20
Example: C strcmp() function
#include <stdio.h>
#include <string.h>
int main() { Output:
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result; strcmp(str1, str2) = 32
// comparing strings str1 and str2 strcmp(str1, str3) = 0
result = strcmp(str1, str2); strcmp(str2, str3) = -32
printf("strcmp(str1, str2) = %d\n", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
// comparing strings str2 and str3
result = strcmp(str2, str3);
printf("strcmp(str2, str3) = %d\n", result);
return 0;
}
21
strrev() Function
• strrev() function returns reverse of the given string.
• Syntax:
strrev(string);
• Function strrev() returns the same string in reverse order, ie. it does
not creates a new string, instead, it reverses the original string and
returns the same.
Example: C strrev() function
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Hello world"; // initialzing a char array
printf("The string is : %s\n", str); // printing the Output:
actual array The string is : Hello world
strrev(str); // reversing the char array The string after using function
strrev() is : dlrow olleH
printf("The string after using function strrev() is :
%s\n", str); // printing the reversed array
return 0;
}
#include <stdio.h>
#include <string.h> case 2:
int main() { printf("Enter the First String: ");
int s, length; scanf("%s", str1);
char str1[100], str2[100]; printf("Enter the Second String: ");
char choice; scanf("%s", str2);
do { strcpy(str2, str1);
printf(“Built in String Functions\n"); printf("String1: %s\n", str1);
printf("1. String Length: strlen() \n2. String Copy: strcpy() \n3. printf("The copied string2 is: %s\n", str2);
String Concat: strcat()\n4. String Compare: strcmp()\n"); break;
printf("Enter your choice: ");
scanf("%d", &s); case 3:
printf("Enter the First String: ");
switch (s) { scanf("%s", str1);
case 1: printf("Enter the Second String: ");
printf("Enter a String: "); scanf("%s", str2);
scanf("%s", str1); strcat(str1, str2);
length = strlen(str1); printf("The Concatenated String: %s\n", str1);
printf("The length of the string is: %d\n", length); break;
break;
case 4:
printf("Enter the First String: ");
scanf("%s", str1);
printf("Enter the Second String: ");
scanf("%s", str2);
// Ask if the user wants to continue
if (strcmp(str1, str2) != 0)
printf("Do you want to continue? (y/n): ");
printf("The strings are not equal\n");
scanf(" %c", &choice);
else
} while (choice == 'y' || choice == 'Y');
printf("The strings are equal\n");
break;
return 0;
case 5:
}
printf(“Enter the string:”);
scanf(“%s”,str1);
strrev(str1);
printf(“Reversed String:%s”,str1);
break;
default:
printf("Invalid choice\n");
}
Thank You!!!