STRING IN C
⮚ INTRODUCTION
⮚ MANIPULATION OF STRING
• Initialize string
• Print string
• Read string from user
⮚ POINTERS AND STRINGS
⮚ STANDARD LIBRARY STRING FUNCTIONS
⮚ PROGRAMS FOR USER DEFINED STRING FUNCTIONS
STRING
String in C language is defined as an array of characters which ends with a null character.
char c[10]; Array of characters
0 1 2 3 4 5 6 7 8 9 Every string in C
c H E L L O D E A R \0 ends with a NULL
character.
NULL CHARACTER
int a[10]; Array of integers
float b[10]; Array of floating numbers
INTITIALIZE A STRING
char s[5] = {‘U’, ‘I’, ‘I’, ‘T’, ‘\0’}; 0 1 2 3 4
char s[ ] = {‘U’, ‘I’, ‘I’, ‘T’, ‘\0’}; s U I I T \0
s[0] s[1] s[2] s[3] s[4]
char s[5] = “UIIT”; // ‘\0’ Not neccessary
char s[ ] = “UIIT”; // ‘\0’ Not neccessary
Strings do not support assignment operator once they are declared or initialized.
char s[10]; // String declaration
s[10] = “UIITCLASS”; //Error !
char str[4] = “CSE”; // String initialized
str[4] = “ECE”; //Error !
For this purpose, you have to use strcpy() function which is defined in string.h
header file.
This method doesn’t rely upon string length.
HOW TO PRINT A STRING ?
//Using printf() function // Print using \0 (NULL Charcater)
void main() void main()
{
{
char str[20] = “PROGRAMMING”;
char str[20] = “PROGRAMMING”; int i=0;
Output:
int i=0; while(str[i] != ‘\0’)
PROGRAMMING
while(i<=10) {
{ printf(“%c”, str[i]);
i++;
printf(“%c”, str[i]); //void
Print string using pointer
main() }
i++; {
char str[20] = “PROGRAMMING”; getch();
} char *ptr;
ptr=str;
while(*ptr != ‘\0’)
}
getch(); {
printf(“%c”, *ptr);
} ptr++;
}
getch();
}
READ STRING FROM THE USER
//Using scanf() function
void main() scanf() function does not support whitespaces (space, tab, newline etc).
{ i.e. it is incapable of receiving multi-word strings.
char name[20];
Output:
printf(“Enter Your Name: ”); Enter Your Name: Aman Kumar
scanf(“%s”, name); You have entered Aman
Aman Kumar
printf(“You have entered %s”,name);
getch();
}
//Using gets() function
gets(name);
puts() function can’t display more than one string at a time. puts(“You have entered ”);
Also unlike printf() function, puts() function places the cursor on the next line. puts(name);
getch();
}
POINTERS AND STRINGS
Suppose we wish to store “UIIT”.
There are two methods:
• Store it in a string. e.g. char s[ ] = “UIIT”;
• Store it in some location in memory and assign its address to a
character pointer. e.g. char *ptr = “UIIT”
Difference is, we can’t assign string to another string. But we can assign a char pointer
to another char pointer.
void main() Also unlike string, char pointer can be re-initialized .
{
char str1[20] = “PROGRAM”; void main()
char str2[20]; {
char *ptr1 = “CODE”; char str1[ ] = “PROGRAM”;
char *ptr2; char *ptr1 = “CODE”;
str2 = str1; // Error str1 = “SAVE”; // Error
ptr1 = ptr2; // Valid ptr1 = “SAVE”; // Valid
} }
STANDARD LIBRARY STRING FUNCTIONS
Those functions which are stored in <string.h> header file are known as
standard library string functions.
Some important usable functions for string manipulation:
strlen() - To find length of a string e.g. strlen(string);
strcpy() - To copy a string to another string e.g. strcpy(target, source);
strncpy() - To copy a first n characters of a string to another string e.g. strncpy(target, source, n);
strcat() - To concatenate two strings e.g. strcat(target, source);
strncat() - To append first n characters of a string at the end of another strings e.g. strncpy(target, source,
n);
strcmp() - To compare two strings e.g. strcmp(string1, string2);
strncmp() - To compare first n characters of two strings e.g. strcmp(string1, string2, n);
strchr() - To find an occurrence of a character in string e.g. strchr(string, ch);
strstr() - To find an occurrence of a string in another string e.g. strstr(string1, string2);
strlwr() - To convert a string to lowercase e.g. strlwr(string);
strupr() - To convert a string to uppercase e.g. strupr(string);
strrev()- To reverse a string e.g. strrev(string);
strdup() – To find duplicate of a string e.g. strdup(string);
USER DEFINED STRING FUNCTIONS
Those functions which are defined by the user to manipulate string as per
requirement are known as user defined string functions.
Some important usable functions for string manipulation:
xstrlen() - To find length of a string e.g. int xstrlen(char *);
xstrcpy() - To copy a string to another string e.g. void xstrcpy(char *, char *);
xstrcat() - To concatenate two strings e.g. void xstrcat(char *, char *);
xstrcmp() - To compare two strings e.g. xstrcmp(char *, char *);
xstrrev()- To reverse a string e.g. char* xstrrev(char *);
PROGRAM IMPLEMENTATION
• Lets do it in Compiler.