Presentation is loading. Please wait.

Presentation is loading. Please wait.

Strings in C Array of characters is called a string.

Similar presentations


Presentation on theme: "Strings in C Array of characters is called a string."— Presentation transcript:

1 Strings in C Array of characters is called a string.
A string is terminated by a null character \0. Ex: “C String” Here “C string” is a string. When compiler encounters a string, it appends \0(null character) at the end of the string. C S t r i n g \0

2 Declaration of strings
Strings are declared in a similar manner as arrays. Only difference is that, strings are of char data type. char s[5];

3 Initialization of strings
In C, string can be initialized in a number of different ways. For convenience and ease, both initialization and declaration are done in the same step. char c[] = "abcd"; OR, char c[50] = "abcd"; char c[] = {'a', 'b', 'c', 'd', '\0'}; char c[5] = {'a', 'b', 'c', 'd', '\0'}; a b c d \0

4 Reading Strings from user
Use scanf() to read a string like any other data type. The scanf() function only takes the first entered word. The function terminates when it encounters a space. Reading words from the user char c[20]; scanf(“%s”,c);

5 Using scanf() to read a string
C program to illustrate how to read string from terminal. Sample output: Enter name: Mani Ratnam Your name is Mani void main() { char name[20]; printf("Enter name: "); scanf("%s",name); printf("Your name is %s",name); }

6 Using getchar() to read a line of text
C program to read a line of text characters. Sample output: Enter name: Mani Ratnam Your name : Mani Ratnam void main() { char name[30], ch; int i=0; printf("Enter name: "); while(ch!=‘\n’) ch=getchar(); name[i]= ch; i++; } name[i]=‘\0’; printf("Your name : %s",name); Using the function getchar(), ch gets a single character from the user each time. This process is repeated until the user enters return (enter key). Finally, the null character is inserted at the end to make it a string.

7 Using gets() and puts()to read a line of text
C program to read a line of text characters. Sample output: Enter name: Mani Ratnam Your name : Mani Ratnam void main() { char name[30]; printf("Enter name: "); gets(name); printf(“Your name: "); puts(name); } Using the function gets(), a series of characters are received until user press enter. The compiler insert the null character at the end to make it a string.

8 Passing string to a function
#include <stdio.h> void displayString(char str[]); void main() { char str[50]; printf("Enter string: "); gets(str); displayString(str); // Passing string c to function. } void displayString(char str[]) printf("String Output: "); puts(str); Strings are just char arrays. So, they can be passed to a function in a similar manner as arrays.

9 String Handling Functions
There are various string operations you can perform manually like: finding the length of a string, concatenating (joining) two strings etc. But, for programmer's ease, many of these library functions are already defined under the header file <string.h>

10 String Handling Functions
String Length Function

11 strlen(string) #include <stdio.h> #include <string.h>
It returns the length of the string without including end character (terminating char ‘\0’). #include <stdio.h> #include <string.h> void main() { char str1[20] = "Book"; printf("Length of string str1: %d", strlen(str1)); }

12 Length of the string without using built in function
#include <stdio.h> void main() { char string[50]; int i, length = 0; printf("Enter a string \n"); gets(string); /* keep going through each character of the string till its end */ for (i = 0; string[i] != '\0'; i++) length++; } printf(“The length of %s = %d\n", string, length);

13 strlen vs sizeof strlen returns you the length of the string stored in array,
sizeof returns the total allocated size assigned to the array.

14 strnlen(string , maxlength)
It returns length of the string if it is less than the value specified for maxlen (maximum length) otherwise it returns maxlen value. #include <stdio.h> #include <string.h> void main() { char str1[20] = “Record Book"; printf("Length of string str1 when maxlen is 30: %d", strnlen(str1, 30)); printf("Length of string str1 when maxlen is 10: %d", strnlen(str1, 10)); } Output: Length of string str1 when maxlen is 30: 11 Length of string str1 when maxlen is 10: 10

15 String Handling Functions
String Comparing Function

16 strcmp(string1, string2)
If string1 < string2 OR string1 is a substring of string2 then it would result in a negative value. If string1 > string2 then it would return positive value. If string1 == string2 then you would get 0(zero) when you use this function for compare strings.

17 #include <stdio.h>
#include <string.h> void main() { char s1[20] = “Record"; char s2[20] = “Record Book"; if (strcmp(s1,s2)==0) printf("string 1 and string 2 are equal"); } else printf("string 1 and 2 are different");

18 /*Program to compare Strings without using built in function */
 void main() {    char str1[30], str2[30];    int i;     printf("\nEnter two strings :");    gets(str1);    gets(str2);     i = 0;    while (str1[i] == str2[i] && str1[i] != '\0')      {  i++; }    if (str1[i] > str2[i])       printf("str1 > str2");    else if (str1[i] < str2[i])       printf("str1 < str2");    else       printf("str1 = str2");   } /*Program to compare Strings without using built in function */

19 strncmp(string1, string2, n)
It compares both the string till n characters or in other words it compares first n characters of both the strings. void main() { char s1[20] = “Record Book"; char s2[20] = “Record work"; /* below it is comparing first 6 characters of s1 and s2*/ if (strncmp(s1, s2, 6) ==0) { printf("string 1 and string 2 are equal"); else printf("string 1 and 2 are different"); } Output: string1 and string 2 are equal

20 String Handling Functions
String Concatenation Function

21 strcat(string1, string2)
It concatenates two strings and returns the concatenated string. That is, it attaches the string 2 to the RHS of the string 1. int main() { char s1[10] = "Hello"; char s2[10] = "World"; strcat(s1,s2); printf("string after concatenation: %s", s1); return 0; } Output : string after concatenation: HelloWorld

22 /*Program to concatenate Strings without using built in function */
void main(void) { char str1[25],str2[25]; int i=0, j=0; printf("\nEnter First String:"); gets(str1); printf("\nEnter Second String:"); gets(str2); while(str1[i]!='\0') i++; while(str2[j]!='\0') { str1[i]=str2[j]; j++; } str1[i]='\0'; printf("\nConcatenated String is %s",str1); /*Program to concatenate Strings without using built in function */

23 strncat(string1, string2, n)
It concatenates n characters of str2 to string str1. A terminator char (‘\0’) will always be appended at the end of the concatenated string. int main() { char s1[10] = "Hello"; char s2[10] = “1234"; strncat(s1,s2, 3); printf("Concatenation using strncat: %s", s1); return 0; } Output: Concatenation using strncat: Hello123

24 String Handling Functions
String Copy Function

25 strcpy(string1, string2)
It copies the string str2 into string str1, including the end character (terminator char ‘\0’). void main() { char s1[30] = “hi"; char s2[30] = “programmer"; /* this function has copied s2 into s1*/ strcpy(s1,s2); printf("String s1 is: %s", s1); } Output: String s1 is: programmer

26 /*Program to copy Strings without using built in function */
void main() { char s1[100], s2[100], i; printf("Enter string s1: "); scanf("%s",s1); for(i = 0; s1[i] != '\0'; ++i) s2[i] = s1[i]; } s2[i] = '\0'; printf("String s2: %s", s2); /*Program to copy Strings without using built in function */

27 strncpy(string1, string2, n)
Case1: If length of str2 > n then it just copies first n characters of str2 into str1. Case2: If length of str2 < n then it copies all the characters of str2 into str1 and appends several terminator chars(‘\0’) to accumulate the length of str1 to make it n. void main() { char first[30] = “hello”; char second[30] =“Amrita”; /* this function has copied first 10 chars of s2 into s1*/ strncpy(s1,s2, 5); printf("String s1 is: %s", s1); } Output: String s1 is:Amrit

28


Download ppt "Strings in C Array of characters is called a string."

Similar presentations


Ads by Google