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

M3 S7 Strings

1. String is an array of characters terminated by a null character. Strings can be declared and initialized similar to arrays in C. 2. Common functions for manipulating strings include strlen() to get the length, strcpy() to copy one string to another, strcmp() to compare two strings, and strcat() to concatenate two strings. 3. Other useful functions include those that convert case (toupper(), tolower()), reverse a string (strrev()), and trim or truncate copies (strncpy()). Character handling functions allow testing and converting individual characters.

Uploaded by

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

M3 S7 Strings

1. String is an array of characters terminated by a null character. Strings can be declared and initialized similar to arrays in C. 2. Common functions for manipulating strings include strlen() to get the length, strcpy() to copy one string to another, strcmp() to compare two strings, and strcat() to concatenate two strings. 3. Other useful functions include those that convert case (toupper(), tolower()), reverse a string (strrev()), and trim or truncate copies (strncpy()). Character handling functions allow testing and converting individual characters.

Uploaded by

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

Strings

Dr. Karuna C. Gull


Strings
• String is an Array of Characters terminated by
a NULL character '/0'.
• Example:
char a[10] = “Hello”;
• The above string can be pictorially
represented as shown below:
Declaration of String
• Strings are declared in C in similar manner as arrays. Only
difference is that, strings are of char type.
char string_name[size];
•  The size determines the number of characters in the
string name including the null character.
• Example:
char s[5]; //string s can hold maximum of 5 characters
including NULL character
• The above code can be pictorially represented as shown
below:
Initialization of String
• 1. Compile time Initialization
• Example:
char s[4]={'V', 'T', 'U' ,’\0’};
or
char s[4]=“VTU”;
• The above code can be pictorially represented
as shown below:
Initialization of String
• 2. Run time Initialization
• Reading a string using scanf():
The conversion specification for reading a
string using scanf is %s.
One must not use ampersand (&) symbol
while reading the string.
char stringname[30];
scanf(“%s”,stringname);
Reading a string using scanf( )
• Example :
#include<stdio.h>
void main ()
{
char name[20];
printf(“\n Enter the name\n”);
scanf(“%s”, name);
}

• Note : The scanf functions stops reading at the first white


space character.
Reading a string using gets( )
• Reading a string using gets( ):
The function gets()is called line oriented I/P
function and can be used to process entire line.
The gets( ) function can be used to read entire
line including white spaces.
• The general form of gets function is as given
below:
• s = gets (str); or gets (str);
Printing a string
• 1. Using printf():
–String is printed using a printf statement.
–As long as the string is terminated by a null character.
–The printf function prints all the character upto but not including the null character.
printf (“ %s”, str);
• Example:
int main()
{
char first[11] = “ANDY”;
printf (“ The name is %s\n”, first);
return 0;
}
• 2. Using puts(): The function puts() is called line oriented O/P function and can be used
to process entire line.
–The puts() function can be used to print the line.
–The general form of puts function is as given below :
puts (str);
Example: Program to count the number of
characters in a string. – String Length
#include <stdio.h>
void main()
{
char str[100];
int i, length;
printf(“\n\tEnter a String to Compute its Length: ");
scanf(“%s", str);
for(i = 0; str[ i ] != ‘\0’; i++) ; /* NULL Statement * /
length = i;
printf(“\n\n\t The Length of String: %s is %d”, str, length);
}
Character Handling Library Functions
• Character handling Functions Includes functions to perform
useful tests and manipulations on character data.
• Each function receives a character (or int) or EOF as an
argument.
• These library functions exists in the header <ctype.h>.
• Commonly used Character handling functions are:
int isdigit( char ) \\ Returns True or False
int isalpha(char ) \\ Returns True or False
int isalnum(char ) \\ Returns True or False
int islower(char ) \\ Returns True or False
int isupper(char ) \\ Returns True or False
ch = char tolower( int ) \\ Returns a character
ch = char toupper( int ) \\ Returns a character
Example: Program to Convert lower case
character to upper case and vice versa.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("Enter a Character : ");
scanf("%c", &ch);
if(islower(ch))
{
ch = toupper(ch);
}
else
{
ch = tolower(ch);
}
printf("\n\tCharacter After Reversing case is : %c", ch);
return 0;
}
Example: Program to Check whether the
character read is digit or an alphabet.
#include<ctype.h>
#include<stdio.h>
int main()
{
char ch;
printf("Enter a Character: ");
scanf("%c",&ch);
if(isdigit(ch))
{
printf("\n\t Character %c is a Digit", ch);
}
if(isalpha(ch))
{
printf("\n\tCharacter %c is a Alphabet", ch);
}
return 0;
}
String Manipulation Functions From The
Standard Library
• C language provides number of built in string handling functions.
• All the string manipulation functions are defined in the standard library
<string.h>.
• The standard library string.h contains many functions for string
manipulation.
• Some of the string handling or manipulation functions used in C are:
1. strlen( )
2. strcpy( )
3. strncpy( )
4. strcmp( )
5. strcat( )
6. strlwr( )
7. strupr( )
8. strrev( )
String Manipulation Functions From
The Standard Library
1. strlen( ) :
• This function is used to find the length of the string in bytes. The
general form of a call to strlen is the following :
length = strlen(str);
• The parameter to strlen , str is a string .
• The value it returns is an integer representing the current length of
str in bytes excluding the null character.
• Example:
#include<string.h> Output
char str[20]; Enter a string
Program
int l; Length of the given string is 7
scanf(“%s”,str);
l = strlen(str);
String Manipulation Functions From The
Standard Library
2. strcpy( ) :
• This function copies the string from one variable to another variable.
• The strcpy( ) function will copy the entire string including the
terminating null character to the new location.
• General form of a call to strcpy :
strcpy(dest,source) ;
• Here the characters in source string will get copied to destination
string.
• Example :
Output
#include<string.h>
Enter first string
char str1[10],str2[10]; Program
printf (“\n Enter first string \n”); The value of str1 : Program
scanf(“%s”,str1); The value of str2 is : Program
strcpy(“str2”,str1);
String Manipulation Functions From The
Standard Library
3. strncpy() :
• This function copies the string from one variable to another variable, but
only upto the specified length .
• The general form of a call to the strncpy function is the following :
strncpy(dest, source,n) ;
• Where.
– dest –> Destination string
– source -> source string
– n-> is the number of characters
• Example : Output
#include<string.h> Enter a string
char str1[10],str2[10]; PROGRAM
printf(“\n Enter a string \n”); The value of str1 : PROGRAM
scanf(“%s”,str1); The value of str2 is : PROGR
strncpy(str2,str1,5);
String Manipulation Functions From The
Standard Library
4. strcmp( ) :
• It is used to compare one string with another and it is case
sensitive .
• The general form of the call to strcmp is the following where
either parameter may be a string literal or variable :
• result = strcmp(first, second);
• The function strcmp returns an integer determined by the
relationship between the strings pointed to by the two
parameters.
• The result may take any of the following value :
– result > 0 if first > second
– result = 0 if first = =second
– result < 0 if first < second
4. strcmp() - Example :
#include<stdio.h>
#include<string.h>
main( ) Output
{ Enter string one : Rahul
char str1[30]=“Rahul”,str2[30]=“Modi”; Enter string two : Modi
int n; Strings are not equal
n = strcmp(str1,str2);
if(n= =0)
printf(“ Both the strings are equal
\n”);
else
printf(“\n Strings are not equal \n”);
}
String Manipulation Functions From The
Standard Library
• 5. strcat( ):
• This function is used to join (concatenate) two strings .
• The resulting string has only one null character at the end.
• The general form of the call to the strcat function is the
following, where both first and second are pointers to strings :
strcat(first, second);
• Example:
#include<string.h>
char first[7] = “Fri”;
char second[7]=”day”;
strcat(first, second);
String Manipulation Functions From The
Standard Library
6. strlwr( ) :
• This function converts uppercase characters to lowercase .
• The general syntax is
Output
strlwr(str) KARAVALI
• Example: The Entered string in lower case
#include<stdio.h> characters is
#include<string.h> karavali
main()
{
char upr[30]=“KARAVALI”;
puts(upr);
strlwr(upr);
printf(“\n The Entered string in lower case characters is %s\n”,upr);
}
String Manipulation Functions From The
Standard Library
7. strupr( ) :
• This function converts lowercase characters to uppercase .
• The general syntax is Output
strupr(str) . hello
The Entered string in upper case
• Example:
characters is : HELLO
#include<stdio.h>
#include<string.h>
main()
{
char lwr[30]=“hello”;
puts(lwr);
strupr(lwr);
printf(“\n The Entered string in upper case characters is :%s\n”,lwr);
}
String Manipulation Functions From The
Standard Library
8. strrev() :
• This function reverses a given string .
• The general syntax is
strrev(str) .
Example:
#include<stdio.h>
Output
#include<string.h> Enter a string
main() RAMA
{ The Entered string in Reversed format is AMAR

char a[30]=“RAMA”;
puts(a);
strrev(a);
printf(“\n The Entered string in Reversed format is %s\n”,a);
}
Two dimensional array of characters

Syntax:
char stringname[rowsize][colsize];
Two dimensional array of characters

char s[3][50] = { “EDUSAT”, “PROGRAM”,


“BANGALORE” };
Two dimensional array of characters
Example: Program to Read a List of Names
#include<stdio.h>
int main()
{
int i , n; char names[10][50];
printf("\n\tEnter the Number of Names: ");
scanf("%d", &n);
printf("\n\tEnter %d Names", n);
for(i = 0; i < n; i++)
{
printf("\n\tEnter Name-%d: ", i+1);
scanf("%s", names[i]);
}
printf("\n\tThe %d Names are: ", n);
for(i = 0; i < n; i++)
printf("\n\t\t\t%s",names[i]);
return 0;
}
Thank you
End of Module -3

You might also like