1 / 23

String Array (Multidimensional Arrays)

String Array (Multidimensional Arrays) 1 . A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string]; For ex: char s[2][40] = {“I love ice cream”,”I love India”};

calla
Download Presentation

String Array (Multidimensional Arrays)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string]; For ex: char s[2][40] = {“I love ice cream”,”I love India”}; (A) The number of strings is optional if the string array is initialized with the declaration India};

  2. (B) The number of string is a must of initialization of string is done after declaration. For ex: char s[2][40]; strcpy(s[0], “I love ice cream”); strcpy(s[1], “I love India”); In this case strcpy is the only way you can initialize the individualstring in the string array. 2. Inputting values in a sting Array:

  3. You can use a scanf() or gets() to accept the value in a string array For ex: char s[2][40]; int i; for(i=0; i<2; i++) { printf(“\n\n Enter the %d string”,i); scanf(“%s”, s[i]); }

  4. (or) char s[2][40]; int i; for(i=0; i<2; i++) { printf(“\n\n Enter the %d string”, i); gets(s[i]); }

  5. 3. Printing the string array: The printing of the string array can be accompanied by using puts() or printf(). For ex: char s[2][40] = {“I love ice cream”,”I love Jamaica”}; int i; for(i=0; i<2; i++) { printf(“%s”, s[i]); }

  6. (or) char s[2][40] = {“I love ice cream”,”I love Jamaica”}; int i; for(i=0; i<2; i++) { puts(s[i]); }

  7. 4. String Functions: C offers a variety of functions for string manipulations like: Determine the length of string Copy and joining strings Compare strings Search strings Convert strings 4.1 Length of string using strlen:

  8. Syntax: strlen(stringname); For ex: int l = strlen(s); 4.2 strcpy() Syntax: strcpy(Destination string, source string); This copies the contents of the string pointed by source into location pointed by destination.

  9. For ex: strcpy(s,”Welcome to Jamaica”); strcpy(s, s1); 4.3strncpy() This lets you specify how many characters to copy from source to destination. The syntax is: strncpy(char * destination, char * source, size); char dest[10] = “Jamaica”; char source[9] = “I love”; int n = 1; For ex: strncpy(dest, source, n);

  10. 4.4 strcat(): This function concatenates two strings. The syntax is: strcat(char * str1, char * str2); This appends a copy of str2 onto the end of str1, moving the terminating character to the end of the new string. You must allocate enough space for str1 to hold the resulting string. 4.5strncat() This function concatenates two strings, but it lets you specify how many characters to concatenate. The syntax is: strncat(char * str1, char * str2, size);

  11. For ex: char str[16] = “abcdefghijklmnopq”; char str1[20] “I like”; strncat(str1, str, 3); str1 => “I like abc”; 4.6 strcmp(): This compares two strings. The syntax is: strcmp(char * str1, char * str2);

  12. The resulting values of the comparison is: < 0 str1 is less than str2 0 str1 is equal to str2 > 0 str1 is greater than str2 For ex: char str1[8] = “Jamaica”; char str2[8] = “Jamaica”; if(strcmp(str1,str2) == 0) printf(“\n\n The strings are equal”);

  13. 4.7 strncmp(): This is comparing a specified number of characters of one string to another string. The syntax is: strncmp(char * str1, char * str2, size); The comparison until size character have been compared or end of str1 has been reached. The comparison is case-sensitive. 4.8 strcmpi() or stricmp(): This follows the same as strcmp() or strncmp(), except it is not case-sensitive.

  14. 4.9 strchr(): This function finds the first occurrence of a specified character in a string. The syntax is: strchr(char * str, int ch); The function searches str from left to right until ch is found or null character is found. This returns the address of the first occurrence of the character in the string. The position can be found by subtracting the address of occurrence from the address of the string.

  15. Ex: char str[8] = “Jamaica”; char *loc; int pos; loc = strchr(str, “a”); pos = str - loc;

  16. 4.10 strrchr(): This searches the string for the last occurrence of the specified character in the string. The syntax is: strrchr(char * str, int ch); 4.11 strstr(): This searches the string for the first occurrence of the specified string. The syntax is: strstr(char *str1, char *str2);

  17. 4.12 strlwr(): This function changes the case of string to lower case. The syntax is: strlwr(char *str); 4.13 strupr(): This function changes the case of string to upper case. The syntax is: strupr(char *str);

  18. 4.14 strrev(): This function reverses the order of characters in a string. The syntax is: strrev(char *str); 4.15 strset(): This function changes all the characters in the string to character specified. The syntax is: strset(char *str, int ch);

  19. 4.16 strnset(): This function changes all the characters in the string to character specified, but it changes the first n characters. The syntax is: strnset(char *str, int ch, size);

  20. String to Number Function 4.17 atoi(): This converts a string to an integer. The syntax is: atoi(char *str); For Ex: int a; char c[3] = “123”; a = atoi(c);

  21. 4.18 atof(): This converts a string to type double. The syntax is: atof(char *str); For ex: double a; char c[6] = “123.45”; a = atof(c);

  22. 5. Passing String Array to the Function The string array is passed in the similar multidimensional integer array. In the function prototype, we should pass the array in the following manner: datatype function name (char arrayname[ ][size of individual string elements]); datatype function name(char * arrayname[ ]); datatype function name(char arrayname[No. of strings][size of each string]);

  23. In the calling function in main, the array should be referred to as: function name(array name); In the function definition, the array is declared as: datatype function name(char array name[ ][size]) { } datatype function name(char array name[size][size]) { }

More Related