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

c Programming-chapter 07

This document provides an overview of handling character strings in C programming, including string declaration, initialization, and common operations such as reading, writing, and manipulating strings. It details various string-handling functions like strlen(), strcat(), strcpy(), and their usage with examples. Additionally, it includes assignments and questions to reinforce understanding of string concepts in C.

Uploaded by

flxv07
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)
3 views

c Programming-chapter 07

This document provides an overview of handling character strings in C programming, including string declaration, initialization, and common operations such as reading, writing, and manipulating strings. It details various string-handling functions like strlen(), strcat(), strcpy(), and their usage with examples. Additionally, it includes assignments and questions to reinforce understanding of string concepts in C.

Uploaded by

flxv07
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

WELCOME

PRESENTED BY
SHEYONA G
CHAPTER-07
Handling of Character Strings
A string is a sequence of character enclosed between double quotation marks is known as string constant
or string literal.

Ex: “Have a nice day”


“Hello world 2010”

The common operations performed on character strings include:

1. Reading strings and writing strings


2. Combining strings together
3. Copying one strings to another
4. Comparing strings for equality
5. Extracting a portion of a string
DECLARING AND INITIALIZING STRING VARIABLES

DECLARING STRING VARIABLES:

The general form of declaration of a string variable is:


char string-name[size];

Ex:
char city[10];
char name[20]

When compiler assigns a character string to a character array, it automatically supplies a null character (‘\
0’) at the end of the string.
INITIALIZING STRING VARIABLES:

char city[10]=”New York”;

char city[10]={ ‘N’, ‘e’, ‘w’, ‘ ’, ‘Y’, ‘o’, ‘r’, ‘k’, ‘\0’ };
the size of the string city is 9 bytes and the string “New York” contains 8 elements and one space is
provided for NULL(‘\0’) character.
char str[10] = “GOOD”;
the computer creates a character array of size 10, places the value “GOOD” in it, terminates with the
null character, and initializes all other elements to NULL.
READING STRINGS FROM TERMINAL
Using scanf() function :
The input function scanf() can be used with %s format specification to read in a string of
characters.
For example,
char address[10];
scanf(“%s”,address);

Here ampersand(&) is not required before the variable name.


the scanf() function is that it terminates its input on the first white space it finds. A white space
includes blanks, tabs, carriage returns, form feeds, and new lines.
Example:

Char[5]=“NEW YORK”

N E W \0

If we want to read the entire line “NEW YORK”, it is better to use two arrays of appropriate sizes.
char a[5],b[5];
scanf(“%s%s”,a,b);
will assign the string “NEW” to a and “YORK” to b.
Otherwise use gets() function to read the line of text
WRITING STRINGS TO SCREEN

Using printf() function:


The format %s can be used with printf() to display an array of characters that is terminated by the null
character.
For example,
printf(“%s”,name1); displays the entire array name1.

Otherwise we can use puts() for displaying the string into the screen.
String-handling functions
1. Strlen():
This function is used to find the length of the string.
int n=strlen(string);

Ex:
#include<stdio.h>
#include<string.h> Output:
void main() Enter any string:
{ System
char a[10]; int n; The length of the string is 6
printf("Enter any string:\n");
gets(a);
n=strlen(a);
printf("The length of the string is %d",n);
}
2. Strrev():
This function is used to reverse the given string and the result string will be stored in the same
string variable.
strrev(string);
Example:

#include<stdio.h> Output:
#include<string.h> Enter any string
void main() BCP
{ The reversed string is PCB
char a[10],b[10];
printf("Enter any string\n");
gets(a);
strrev(a);
printf("The reversed string is "); puts(a);
}
3. Strlwr():
This function is used to convert the string from upper case to lower case.
strlwr(string);

Example:
#include<stdio.h> Output:
#include<string.h> Enter any string
void main() BcP
{ The string in lower case is bcp
char a[10],b[10];
printf("Enter any string\n");
gets(a);
strlwr(a);
printf("The string in lower case is "); puts(a);
}
4. Strupr():

This function is used to convert the string from lower case to upper case.
strupr(string);

#include<stdio.h>
#include<string.h>
void main() Enter any string
{ bCp
The string in lower case is BCP
char a[10],b[10];
printf("Enter any string\n");
gets(a);
strupr(a);
printf("The string in upper case is ");
puts(a);
}
5. Strcat():
This function is used to join two strings together.
strcat(string1,string2)
Example:
#include<stdio.h>
#include<string.h> Enter two strings
void main() Very
{ good
char a[10],b[10]; The resultant string is Verygood
printf("Enter two strings\n");
gets(a);
gets(b);
strcat(a,b);
printf("The resultant string is ");
puts(a);
}
6. Strncat():
This will concatenate the left-most n characters of second string at the end of first string. This is a three
parameter string.
strncat(string1,string2,number);
Example:
#include<stdio.h>
#include<string.h> Enter two strings: Bala Gurusamy
void main() Enter the number of characters to be concatenated: 4
{ The resultant string is BalaGuru
char a[10],b[10];
int n;
printf("Enter two strings\n");
gets(a);
gets(b);
printf(“Enter the number of characters to be concatenated: ”);
scanf(“%d”,&n);
strcat(a,b,n);
puts(a);
}
7. Strcpy():
This function is used to copy one string to another.
strcpy(string1, string2);
Here string1 will be act as destination and string2 will be act as source. It means string2 will be copied into string1.
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char a[10],b[10];
printf("Enter any string\n");
gets(a);
strcpy(b,a);
printf("The copied string is ");
puts(b);
}
8. Strncpy():
This function copies only the left most n characters of the source string to the target string. This is a
three parameter function.
strncpy(string1,string2,number);
Example:
#include<stdio.h>
#include<string.h>
Enter two strings
void main()
{
Balaguru
char a[10],b[10]; int n; samy
printf("Enter two strings\n"); Enter the number of characters to be copied: 3
gets(a); The resultant string is Samaguru
gets(b);
printf(“Enter the number of characters to be copied : ”);
scanf(“%d”,&n);
strncpy(a,b,n);
printf("The resultant string is ");
puts(a);
}
9. Strcmp():

This function compares two strings. This comparison is done by taking the numeric difference between the
characters in the strings. So this can return 3 kinds of values. They are
o Zero : if the strings are equal
o Positive value : if the first string is greater
o Negative value : if the second string is greater

It takes the following form,


strcmp(string1,string2);
Example:
#include<stdio.h>
#include<string.h>
void main()
{
char a[10],b[10]; int n;
Enter two strings: their there
printf("Enter two strings : ");
The result is -9
gets(a);
gets(b);
n=strcmp(a,b);
printf("The result is %d ",n);

}
10. Strncmp():
This function compares the left most n characters of strings. This is a three parameter function. This can
return 3 kinds of values.
o Zero : if the strings are equal
o Positive value : if the first string is greater
o Negative value : if the second string is greater

strncmp(string1,string2,number);
Example: Enter two strings: their there
#include<stdio.h> Enter the number of characters to be compared: 3
#include<string.h> The result is 0
void main() Here the first 3 characters of ‘a’ compares with that of ‘b’ and it
{ return ‘0’ because the
char a[10],b[10]; first three characters are same.
int n,x;
printf("Enter two strings : ");
gets(a);
gets(b);
printf(“Enter the number of characters to be compared :
”);
scanf(“%d”,&n);
x=strncmp(a,b,n);
printf("The result is %d ",x);
}
11. Strstr():
This is used to locate a sub string in a string
strstr(string1,string2);
Example: Output:
#include<stdio.h> #include<string.h> Enter first string:
void main() verygood
{ Enter second string:
char a[10],b[10]; good
printf("Enter first string:\n"); Substring found
gets(a);
printf(“Enter second string:\n”);
gets(b);
if(strstr(a,b)==’\0’)
printf(“Substring is not found”);
else
printf(“Substring found”);
}
ASSIGNMENTS
C language does not support ________datatype. string int char float
In C language strings are represented as _______ integer array character array zero float array

What is the output of printf("\"NTTF\"");? NTTF "NTTF" NTTF Error


NULL row and
In a string the last position is reserved for _____ characater row size column size column size
Which format specification is used in scanf() to read a string? %d %ld %f %s
The printf() function may be replaced by ________ function for printing
strings on the monitor. gets() puts() fscanf() getchar()

Which function is used to read a string with white space? gets() putchar() putc() puts()

Which amongst the header file contains gets ( ) function in C language. math.h string.h stdio.h process.h

What is the output of the following?


void main( )
{ char x=’A’; 65 A error 0
printf(“%d”,x);
}

The function strcat ( ) has _______ number of parameters 2 1 3 0


When 2 strings are equal strcmp() function returns _____. 0 1 2 -2
The function strncat ( ) has _______ number of parameters 2 1 3 0

The function strcpy(s2,s1) copies String s1 to s2 String s2 to s1 Does not error


copy
Which function is used to convert a string of digits into their integer atoi() atof() strcmp() strcat()
values?
The _____ function in C language is used to reverse a string. strrev() strlen() strcmp() strcat()
Which amongst the following, function is used for concatenating two strrev() strlen() strcmp() strcat()
string.
Which string function is used to determine the length of the string? strrev() strlen() strcmp() strcat()
Which of the following function is used to convert the string from lower strupr() strlwr() isupper() islower()
to upper case?
Which of the string handling function is used to convert a string into strupr() strlwr() isupper() islower()
lowercase()?
Which of the string handling function is used to compare two strings? compare() comparison() strcp() strcmp()
Which of the string handling function is used to copy one string into copy() strcpy() strcopy() strcopytwo()
another string?
The _____ function in C language is used to find substring in a strstr() strlen() strcmp() strcat()
string.
1) What you mean by a string in C language?
2) How to declare a string variable in C language?
3) Write any 2 example for initializing string variables at compile time in C language.
4) What is a string? How do you initialize string constant at compile time?
5) Write any 2 functions used for reading a string from keyboard.
6) Write any 2 functions used for writing a string to a monitor.
7) Write a C program to read and display your name.
8) Write a C program to find length of a string without using built in function.
9) Write a C program to copy one string to another string without using built in function.
10) What is the difference between gets() and puts() function.
11) What is use of atoi() function in C language?
12) Explain all string handling functions with syntax and example

You might also like