0% found this document useful (0 votes)
44 views12 pages

Unit 3 - String

About strings in C

Uploaded by

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

Unit 3 - String

About strings in C

Uploaded by

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

INTRODUCTION TO

STRINGS

1
Strings
• A string is a collection of the individual array elements or characters.

• String is enclosed within Double quotes.

• “programming" is a example of String.

• Each Character Occupy 1 byte of Memory.

• String is always terminated with NULL Character (‘\0′).


char word[20] = “‘p’ , ‘r’ , ‘o’ , ‘g’ , ‘r’ , ‘a’ , ‘m’ , ‘m’ , ‘i’ , ‘n’ , ‘g’ , ‘\
0’”

2
NULL Character
• NULL Character is also known as string terminating
character.

• It is represented by “\0”.

• NULL Character is having ASCII value 0

• NULL terminates a string, but isn’t part of it

• important for strlen() – length doesn’t include the NULL

3
Declaration of a string
• Since we cannot declare string using String Data Type, instead of which we use
array of type “char” to create String.

• Syntax :

char String_Variable_name [ SIZE ] ;

• Examples :

• char city[30];

• char name[20];

• When we are using string for other purpose than accepting and printing data then
4
you must include following header file in your code– #include<string.h>
Initializing String (Character Array)
• Process of Assigning some legal default data to String is Called
Initialization of String.
• A string can be initialized in different ways. We will explain this
with the help of an example.
• Below is an example to declare a string with name as str and
initialize it with “Welcome”.
1. char str[] = “Welcome";
2. char str[50] = “Welcome";
3. char str[] = {‘W’,'e’,’l’,’c’,’o’,’m’,’e’,'\0’};
4. char str[14] = {‘W’,'e’,’l’,’c’,’o’,’m’,’e’,’\0’};
5
STRING EXAMPLE
#include <stdio.h>

int main ()

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};

printf("Greeting message: %s\n", greeting );

return 0;

Use %s field specification in scanf to read string

Greeting message: Hello


Functions of string.h
Function Purpose Example Output
Strcpy(); Makes a copy of a string strcpy(s1, “Hi”); Copies “Hi” to ‘s1’
variable
Strcat(); Appends a string to the strcat(“Work”, “Hard”); Prints “WorkHard”
end of another string

Strcmp(); Compare two strings strcmp(“hi”, “bye”); Returns -1.


alphabetically
Strlen(); Returns the number of strlen(“Hi”); Returns 2.
characters in a string

Strrev(); reverses a given string Strrev(“Hello”); olleH


Strlwr(); Converts string to Strlwr(“HELLO”); hello
lowercase
Strupr(); Converts string to Strupr(“hello”); HELLO
uppercase
String Copy (strcpy)
• strcpy( ) function copies contents of one string into another string.

• Syntax : strcpy (destination_string , source_string );

• 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.
String Concat (strcat)
• strcat( ) function in C language concatenates (appends) portion of one
string at the end of another string.

• Syntax : strcat( destination_string , source_string, size);

• Example:-strcat(str2,str1,3); – First 3 characters of str1 is concatenated


at the end of str2.

• 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. 9
String Compare (strcmp)
 strcmp( ) function in C compares two given strings and returns zero if they
are same.
 If length of string1 < string2, it returns < 0 value that is -1.

 If length of string1 > string2, it returns > 0 value that is 1

 If length of string1 = string2 it returns 0.

Syntax : strcmp (str1 , str2 );strcmp( ) function is case sensitive. i.e, “A” and
“a” are treated as different characters.

10
String Length (strlen)
• strlen( ) function in C gives the length of the given string.

• Syntax : strlen(str);

• strlen( ) function counts the number of characters in a given


string and returns the integer value.

• It stops counting the character when null character is found. Because,


null character indicates the end of the string in C.

11
• Lab Programs
• Concatenate two strings without built-in functions
• Reverse a string using built-in and without built-in string functions

• https://mitsacin-my.sharepoint.com/:v:/g/personal/
deepthip_mits_ac_in/EfGov8dnlyxFs91YS2cHegwB5Yvy95-
UmTLNwvqg9L0mSw?referrer=Teams.TEAMS-
ELECTRON&referrerScenario=MeetingChicletGetLink.view.view

12

You might also like