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

Pps Unit-II Strings

Uploaded by

Varshith Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Pps Unit-II Strings

Uploaded by

Varshith Reddy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 25

PPS-II

I-B.TECH II-SEM ,VJIT


UNIT-I
STRINGS
1.String Definition
2.String Declaration
3.String Initialization
4.String I/O Functions
5.String Handling functions
6. Character I/O Functions
 In C programming, a string is a sequence of
characters terminated with a null character ’\0’.
 The string can be defined as the one-dimensional
array of characters terminated by a null ('\0'). The
character array or the string is used to manipulate text
such as word or sentences. Each character in the array
occupies one byte of memory, and the last character
must always be’ \0’
 The termination character ('\0') is important in a
string since it is the only way to identify where the
string ends.
How to declare a string?
Declaration of strings: Declaring a string is as simple as
declaring a one-dimensional array. Below is the basic syntax
for declaring a string.
Syntax:
char stringname[size];
ex: char s[5];
char name[20];
String Declaration in c:
How to initialize strings?

Initializing a String: A string can be initialized in different


ways.
While declaring string, size is not mandatory.
Ex: char c[] = {'a', 'b', 'c', 'd', '\0'}; //by char array
(or)
char c[5] = {'a', 'b', 'c', 'd', '\0'};
We can also define the string by the string literal in C language.
Ex: char c[] = "abcd"; //by string literal
(or)
char c[50] = "abcd";
String Initialization in C
Let's take another example:
char c[5] = "abcde";
Here, we are trying to assign 6 characters (the last character is '\
0') to a char array having 5 characters. This is bad and you
should never do this.
What is NULL Char “\0”?
'\0' represents the end of the string. It is also referred as String
terminator & Null Character.
Method 1:
char str[]={‘I', ‘N', ‘D', ‘I', ‘A', '\0'};
Method 2:
The above string can also be defined as –
char str[]=“INDIA";
In the above declaration NULL character (\0) will automatically
be inserted at the end of the string.
String I/O functions in C programming:
The following are the input and output functions of strings in c
1) Input functions: scanf(), gets()
2)Output functions: printf(), puts()
The scanf() and printf() are generic i/o functions that they
support all built-in data types such as int, float, long, double,
strings,..etc. But gets() and puts() are specialized to scan and
print only string data.
the formatted string %s is used in printf() and scanf().
Read & write Strings in C using Printf() and Scanf() functions:
Read String from the user:we can use the scanf() function to
read a string.
The scanf() function reads the sequence of characters until it
encounters whitespace (space, newline, tab, etc.)
Syntax: scanf(“%s”,str1);
printf(“%s”,str1);
Example 1: scanf() to read a string
#include <stdio.h>
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Your name is %s.", name);
return 0;
}
Enter name: Dennis Ritchie
Your name is Dennis.
How to read a line of text?
we can use the gets() function to read a line of string. And, we
can use puts() to display the string.
Syntax: gets(string name); // %s is not require here
puts(string name); // %s is not require here
Read & Write Strings in C using gets() and puts() functions
#include<stdio.h>
void main()
{
char str[10];
printf("Enter a string");
gets( str );
puts( str ); o/p:Enter a string: Dennis Ritchie

Dennis Ritchie
String Handling Functions in C:
C programming language provides a set of pre-defined functions
called string handling functions to work with string values.
The string handling functions are defined in a header file
called string.h. Whenever we want to use any string handling
function we must include the header file called string.h.
Few commonly used string handling functions are:
Function work of function
strlen() computes string's length
strcpy() copies a string to another
strcat() concatenates(joins) two strings
strcmp() compares two strings
strlwr() converts string to lowercase
strupr() converts string to uppercase
1) strlen( ) Function :
strlen( ) function is used to find the length of a character string.
(or)
The strlen() function returns the length of the given string. It
doesn't count null character '\0'.
Syntax: n = strlen(string name);
It is defined in the <string.h> header file.
Ex: int n;
char st[20] = “Bangalore”;
n = strlen(st);
returns total number of characters in string
o/p: 9
#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]= {'P‘,‘ r', 'o ', 'g‘ ,'r ','a‘ ,'m‘ ,'\0'};
printf("Length of string a = %d \n“ ,strlen(a));
printf("Length of string b = %d \n “,strlen(b));
return 0;
}
o/p: Length of string a = 7
Length of string b = 7
2)strcpy( ) Function :

strcpy( ) function copies contents of one string into another string.

Syntax: strcpy(destination, source);


Example:
strcpy ( str1, str2) – It copies contents of str2 into str1.
strcpy ( str2, str1) – It copies contents of str1 into str2.

• If destination string length is less than source string, entire source string value
won’t be copied into destination string.
For example, consider destination string length is 20 and source string length is 30.
Then, only 20 characters from source string will be copied into destination
string and remaining 10 characters won’t be copied and will be truncated.

Ex: char city[15];


strcpy(city, “BANGALORE”) ;
#include <stdio.h>
#include <string.h>
int main()
{
char str1[20] = "C programming";
char str2[20];
strcpy(str2, str1); // copying str1 to str2
puts(str2);
return 0;
}
o/p:C programming
3) strcat( ) Function :
The strcat() function contcatenates (joins) two strings.(OR) Appends
string2 to string1
The strcat() function takes two arguments:
Destination - destination string
source - source string
• It is defined in the string.h header file.
Example :
• strcat ( str2, str1 ); - str1 is concatenated at the end of str2.
• strcat ( str1, str2 ); - str2 is concatenated at the end of str1.
As you know, each string in C is ended up with null character (‘\0′).
•In strcat( ) operation, null character of destination string is
overwritten by source string’s first character and null character is
added at the end of new destination string which is created
after strcat( ) operation.
#include <stdio.h>
#include <string.h>
int main()
{
char str1[100] = “vjit ";
char str2[] = “college"; // concatenates str1 and str2
strcat(str1, str2);
puts(str1);
puts(str2);
return 0;
}
o/p:
4) strcmp( ) Function :
strcmp( ) function in C compares two given strings and returns
zero if they are same. If length of string1 < string2, it returns <
0 value. If length of string1 > string2, it returns > 0 value.
(or)
The strcmp() compares two strings character by character. If the
strings are equal, the function returns 0.
Syntax : strcmp ( str1, str2 );
strcmp( ) function is case sensitive. i.e., “A” and “a” are treated
as different characters.
Example :
char city[20] = “Madras”;
char town[20] = “Mangalore”;
strcmp(city, town);
#include<stdio.h>
#include <string.h>
int main(){
char str1[20],str2[20];
printf("Enter 1st string: ");
gets(str1);//reads string from console
printf("Enter 2nd string: ");
gets(str2);
if(strcmp(str1,str2)==0)
printf("Strings are equal");
else
printf("Strings are not equal");
return 0;
}
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
return 0;
}
5.strrev()Function:
The strrev(string) function returns reverse of the given string.
Syntax:
strrev(string); // It reverses the value of string
#include<stdio.h>
#include <string.h>
void main()
{
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
}
6)strlwr() Function:
The strlwr(string) function returns string characters in lowercase.
Syntax:
strlwr(string);//Converts all the characters of string to lower case.
#include<stdio.h>
#include <string.h>
void main()
{
char str[20];
printf("Enter string: ");
gets(str); //reads string from console
printf("String is: %s",str); o/p:Enter string:VJIT
printf("\nLower String is: %s",strlwr(str)); String is:VJIT
} lower String is:vjit
7 strupr()Function:
The strupr(string) function returns string characters in uppercase.
Syntax:
strupr(string) // Converts all the characters of string to upper
case.
include<stdio.h>
#include <string.h>
void main()
{
char str[20];
printf("Enter string: ");
gets(str);//reads string from console o/p:Enter string:vjit
printf("String is: %s",str); String is:vjit
printf("\nUpper String is: %s",strupr(str)); Upper String is:VJIT
}
Character Input/output functions:
getchar():Reads a single character from the user at the console

Syntax:
char a = getchar();
putchar(): use to print a single character to the standerd output
The function puts only single character at a time.
All these functions are available in 'stdio.h' header file.
Syntax:
putchar(a);
#include<stdio.h>​
void main()
{
char c;
printf(“Enter the character :”);
c=getchar();
printf(“ entered character is :");​
putchar(c);
}
o/p: Enter the character : A
entered character is:A

You might also like