Вы находитесь на странице: 1из 21

STRINGS

STRINGS
➢A string is a collection of characters terminated by a null
character ‘\0’.
➢Strings are actually one-dimensional array of characters
terminated by a null character '\0'.
➢The null character indicates the end of the string.

➢Each character that composes a string is stored in 1-D


char type arrays in contiguous memory locations.
• char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
OR

• char greeting[] = "Hello";

#include <stdio.h>
int main () {
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
printf("Greeting message: %s\n", greeting );
return 0;
}
For example:
➢The string “Try Again” is a collection of characters ‘T’, ’r’, ’y’, ‘ ‘,
‘A’,’g’,’a’,’i’,’n’ and terminated by ‘\0’.

➢The termination character ‘\0’ is automatically inserted at the


end of the string by the compiler.

➢As strings is a 1-D array of characters so the characters in the


string “Try Again” will be stored in contiguous memory
locations.

T r y A g a i n \0
➢In C, as each character requires 1 bytes for its storage so the total
memory used for storing the string “Try Again” will be 10 (3+1+5+1)
bytes.

➢The strings are always enclosed in double quotes (“ “) and may include
letters, numbers, escape sequences, blank space and some special
characters.
Some valid strings are
“Good Morning”
“Welcome to the world of programming”
“Address=142,Lawrence Road,Jalandhar”
“123-456-789”
Defining String Variables
Just like array of integers or any other data type, the string variables
which are array of characters can be defined using the following syntax:

char string_variable[size];
The string_variable is actually the name of the array and size represents
the maximum number of characters that can stored and it must be a
positive integer value.
For example: character Name of Size
data type array of character specification

char name
[30] ;
Program to input and display a string entered by the user
#include<stdio.h>
main()
{
char name[20];
printf(“\nEnter the name of the person:”);
scanf(“%s”,name);
printf(“\n The entered name was: %s”,name);
}
Output:
Enter the name of the person: Ram
The entered name was: Ram
#include<stdio.h>
main()
{
char name[20];
printf(“\nEnter the name of the person:”);
gets(name);
printf(“\n The entered name was: %s”,name);
}
Output:
Enter the name of the person: Ram Kumar
The entered name was: Ram Kumar
#include<stdio.h>
main()
{
char name[20];
printf(“\nEnter the name of the person:”);
scanf(“%s”,name);
printf(“\n The entered name was: );
puts(name);
}
Output:
Enter the name of the person: Ram Kumar
The entered name was: Ram Kumar
Initializing Strings
The following example shows different ways
of initializing strings:
char name[7]= “PANKAJ”;
OR
char name[7]= {‘P’,’A’,’N’,’K’,’A’,’J’,’\0’};
OR
char name[ ]= “PANKAJ”;
For example: The statement
char name[10]=“PANKAJ”;
In the above initialization, the compiler allocates space for 10 characters, where
first 7 characters are fill with ‘P’,’A’,’N’,’K’,’A’,’J’ and ends with ‘\0’. The remaining 3
spaces of the character array are filled with null characters(‘\0’) automatically. This
method of initialization results in wastage of memory.

P A N K A J \0 \0 \0 \0
A better method of initialization is to omit the size of the character array at the
time of its initialization. In such case, compiler automatically computes the size.
char name[ ] = “PANKAJ”;
Programs to show strings are initialized
int main()
{
char name1[ ] = “PANKAJ”; Output:
char name2[ 10] = “ANSHUMAN”; PANKAJ
char name3[10] = {‘P’,’A’,’N’,’K’,’A’,’J’,’\0’}; ANSHUMAN
printf(“\n Various inputs made are:”); PANKAJ
printf(“%s”,name1);
printf(“%s”,name2);
printf(“%s”,name3);
}
STANDARD LIBRARY STRING HANDLING FUNCTIONS

There are a large number of library functions available in C that allow to operate

strings. Using these operations, the strings can be compared, copied and

concatenated. They all are included in a header file string.h

❑ int strlen() – This function returns the number of characters in a string, not

counting the terminating NULL (‘\0’) character.

Consider z is a int type variable, the statement z=strlen();


To calculate the length of string with using strlen() function
#include<stdio.h>
#include<string.h>
main() Output:
{ Enter a string:
char a[80]; This is a program
int z; Length of string: 17
printf(“\nEnter a string:”);
gets(a);
z=strlen(a);
printf(“\n Length of string:%d”,z);
}
❑ char strcpy (s1,s2): This function copies the string s2 to string s1 stopping after
the terminating NULL character has been moved. It returns the resultant string
s1.
For example: char s1[ ]= “Hello”; char s2[ ]= “Everybody”;
On executing the statement, strcpy (s1,s2);

H e l l o \0

H e l l o E v e r y b o d y \0
To copy a string to another using strcpy()
function
#include<stdio.h>
#include<string.h>
main() Output:
{ Enter a string:
char a[80],n[80]; Programming
printf(“\n Enter a string:”); String after copying
gets(a); Programming
strcpy(n,a);
printf(“\nString after copying:”);
puts(n);
}
❑ char strcat(s1,s2): This function accepting two strings as arguments,
concatenates the second string to the first string. The syntax is
strcat(string1, string2);
strcat(“through”, ”put”);
After executing the above function, the result is throughput
#include<stdio.h> Program to concatenate two strings using strcat() function
#include<string.h> Output
main() Source string = Kumar
{ Target string = Ram Kumar
char source[10]= “Kumar”;
char target [10] = “Ram”;
strcat(target, source);
printf(“\n Source string = %s”,source);
printf(“\n Target string = %s”, target);
}
❑ char strrev(): This function changes all characters in a string to reverse order,
except the terminating null character. The following is the syntax for strrev()
function:
strrev()
To reverse a given string using library function strrev()
#include<stdio.h>
#include<string.h>
main() Output
{ Enter a string: Hi Hello
char a[80]; String before reversed:
printf(“\n Enter a string:”); Hi Hello
gets(a); String after reversing
printf(“\n String before reversed:%s”,a); olleH iH
printf(“\n String after reversing:%s”, strrev(a));
}
strcmp(s1, s2);
• Returns 0 if s1 and s2 are the same;
• less than 0 if s1<s2;
• greater than 0 if s1>s2.

strchr(s1, ch);
• Returns a pointer to the first occurrence of character ch in string s1.

Вам также может понравиться