[go: up one dir, main page]

0% found this document useful (0 votes)
37 views26 pages

Wa0018.

Uploaded by

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

Wa0018.

Uploaded by

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

Procedural Programming

Unit-03

String in C Programming Language


Strings in C Programming
• “A string is a series of characters in a group that occupy contiguous memory or String is
an array of character.”
• Example: “My Training”
“Fundamentals of C Porgramming”

• A string should always be enclosed with in double quotes (“)

• In memory, a string ends with a null character ‘\0’ and it occupies 1 byte of memory.

• Space should be allocated to store ‘\0’ as part of the string

• 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:

char variablename [Number_of_characters ];

• 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’)

/* Declaring a string as a character array */


char a[ ] = “PCU Pune”;
3
Think why ‘\0’ requires only one byte!!
Storage Of Strings In Memory
char acItemCategory[15]= “Books”;

4
Static Initialization of Strings

• char acItemCategory[15]=“Greeting Cards”;


• In the above declaration, the size of the array is specified according to the number of
characters in the string. One extra space for ‘\0’
• char acItemCategory[15] =“Ornaments” ;
• Here the size is more than the number of characters which is valid.
• char acItemCategory[ ]=“Groceries”;
• Here the size of the array is computed automatically which is 10. Total number of
characters in the string is 9 and 1 for ‘\0’;

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

• /*Program to accept Item Category and display*/


#include<stdio.h>
int main()
{ char str[15]; //character array declaration
printf("Enter the category code: ");
scanf("%s",str);
printf("The given Item Category is %s", str);
return 0;
}

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.

printf("Length of string b = %d \n",strlen(b));


printf("Length of string c = %d \n",strlen(c));
return 0;
}

11
strcpy() Function

• strcpy() function is used to copy one string to another


• Syntax:
strcpy (Dest_String , Source_String);
• Here Dest_string should always be variable
• Source_String can be a variable or a string constant

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”);

• The resultant string in ‘Course’ will be


• “The course is 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!!!

You might also like