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

Character Arrays and Strings in C

The document provides an overview of character arrays and strings in C programming, detailing their declaration, initialization, reading, writing, and various string functions. It explains memory representation, string handling functions from the <string.h> library, and arithmetic operations on characters. Key functions such as strlen, strcpy, strcat, and strrev are highlighted for string manipulation.

Uploaded by

gauravis147
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)
12 views26 pages

Character Arrays and Strings in C

The document provides an overview of character arrays and strings in C programming, detailing their declaration, initialization, reading, writing, and various string functions. It explains memory representation, string handling functions from the <string.h> library, and arithmetic operations on characters. Key functions such as strlen, strcpy, strcat, and strrev are highlighted for string manipulation.

Uploaded by

gauravis147
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

Presentation on Character

Array & Strings

.
Overview
Character Array & Strings
1. Declaration of a string.
2. Initialization of a string.
3. Reading strings.
4. Writing strings.
5. String Functions
6. Arithmetic Operations
Character Arrays and Strings and Their Uses
• A string is represented using a character array and is always
terminated with the null character ‘\0’
• A string is a sequence of characters that is treated as a single data
item.
• Strings are actually one-dimensional array.
• Character strings are often used to build meaningful and readable
programs.
Memory Representation of String
Index
0 1 2 3 4 5 6 7 8 9 10 11
value
T E A M A M P H A N \0
Address
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012

• 12 bytes of memory is allocated to store 12 characters.


Declaring
• C allows to represent strings as character arrays rather than
strings.
• A string variable is any validC variable name and is always
declared as an array of characters.
• The general form of declaration of a string variable is
char string_name[ size ];
• Here, string_name is any name given to string variable
• size is used to define the length of the string or the number
of characters in the string.
• The size should be equal to the maximum numbers of characters
in the string plus one.
Initializing A String

• A string can be initialized in several


ways char str[ ] = “Team Amphan”;
char str[12] = “Team Amphan” ;
char str[ ] = {‘T’, ‘e’, ‘a’, ‘m’, ‘ ’, ‘A’, ‘m’, ‘p’, ‘h’, ‘a’, ‘n’, ‘\0’ } ;
char str[12] = {‘T’, ‘e’, ‘a’, ‘m’, ‘ ’, ‘A’, ‘m’, ‘p’, ‘h’, ‘a’, ‘n’, ‘\0’ } ;
Reading String from Terminal
• Strings can be read in several ways using-
scanf() getchar()gets()
Syntax-
char string_name [string_size];
scanf(“%ws”, string_name);
• %s and %ws can read only strings without whitespaces.
• Using getchar
char ch;
ch=getchar();//no parameter
• Using gets
gets(str); //one parameter
• %[. .] can be used to read a line containing a variety of characters, including
whitespaces.
• Using Line of Text
char a [80];
scanf(“%[^\n]”, a);
Writing Strings to Screen
• Strings can be written in several ways
using- printf() putchar() puts()
• Using printf function
printf(“%s”, string_name);
• C uses putchar to output the values of character variables.
char ch =‘A’;
putchar (ch); \\equivalent to printf(“%c”, ch);
• Another convenient way of printing string values is to use the function
puts. puts(str);
Char str[12]=“Team Amphan”

9
String Handling Functions
The header file <string.h> contains many string manipulation
functions. Such as-
Function Name Function Action/Purpose
String Length strlen Get string length.
String Copy strcpy Copy a string.
String Concatenation strcat Concatenate two strings.
String Compare strcmp Compare two strings.
String Reverse strrev Return the string reversed.
Lower to Upper strupr Convert to upper case.
Upper to Lower strlwr Convert to lower case.
Character Existence strchr Searching a character.
String Length
• This function counts and returns the number of characters in a
string. The counting ends at the first null character.
int n = strlen(string);
String Copy

• Syntax-
strcpy(string1,string2);
Here, string1 is the destination string and string2 is the source
string.
strncpy(string1, string2, n);
This function copies only the left-mostn characters of
the source string to the destination string.
Copying A String
Comparison of Two Strings
• Syntax-
strcmp(string1,string2);
Here, string1 is the destination string and string2 is the source string.
strncmp(string1, string2, n);
This function compares the left-most n characters of the source string
and the destination string and returns.
It returns integer value which includes
0- equal
Positive – s1>s2
Negative s1<s2
When return value is negative When return value is positive
Comparing Strings
• Syntax-
• strncmp(string1, string2, n);
String Concatenation
• Syntax-
strcat(string1,string2);
Here, string1 is the destination string and string2 is the source string.
• It adds second string at the end of the first string.
strncat(string1, string2, n);
This function concatenates only the left-most n characters of the source
string at the end of destination string.
String Concatenation
Converting Cases
• Syntax
strupr(string_name);
This function converts the lower case letters of the string into upper case letters.
• Syntax
strlwr(string_name);
This function converts the upper case letters of the string into lower case letters.
String Reverse
• Syntax
strrev(string_name);
This function reverses the character string.
Character Existence in String

• It searches string string1 for character ch.


• strchr(string1, ‘ch’);
This function will locate the first occurrence of the
character ‘ch’ and the call.
• strrchr(string1, ‘ch’);
This function will locate the last occurrence of the
character ‘ch’ and the call.
Character Existence in String
String Subset
• Syntax
strstr(string1,string2);
• This function can be used to locate a sub-string in a string.
• This function searches the string string1 to see whether the
string string2 is contained in string1. If yes, the function
returns the position of the first occurrence of the sub-string.
Otherwise, it returns a null pointer.
Arithmetic Operations on Characters
• Way 1: Displays ASCII value[ Note that %d in Printf ]
char x = 'a’;
printf("%d",x); // Display Result = 97
• Way 2 : Displays Character value[ Note that %c in Printf ]
char x = ‘a’;
printf("%c",x); // Display Result = a
• Way 3 : Displays Next ASCII value[ Note that %d in Printf ]
char x = 'a' + 1 ;
printf("%d",x);// Display Result = 98 ( ascii of 'b' )
Arithmetic Operations on Characters
• Way 4 Displays Next Character value[Note that %c in Printf ]
char x = 'a' + 1;
printf("%c",x); // Display Result = 'b'
• Way 5 : Displays Difference between 2 ASCII in Integer[Note %d in Printf ]
char x = 'z' - 'a’;
printf("%d", x); /* Display Result = 25 (difference between ASCII of z and a ) */
• Way 6 : Displays Difference between 2 ASCII in Char [Note that %c in Printf ]
char x = 'z' - 'a';
printf("%c", x); /* Display Result = ↓ ( difference between ASCII of z and a ) */
• The C library supports a function that converts a string of digits into their integer
values.
x=atoi(string);
Arithmetic Operations on Characters

You might also like