23
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”}; India};

String Array (Multidimensional Arrays)

  • Upload
    calla

  • View
    82

  • Download
    1

Embed Size (px)

DESCRIPTION

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”}; - PowerPoint PPT Presentation

Citation preview

Page 1: 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”};

(A) The number of strings is optional if the string array is initialized with the declaration

India};

Page 2: String Array (Multidimensional Arrays)

(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 individual string in the string array.

2. Inputting values in a sting Array:

Page 3: String Array (Multidimensional Arrays)

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]);

}

Page 4: String Array (Multidimensional Arrays)

(or)

char s[2][40];

int i;

for(i=0; i<2; i++)

{

printf(“\n\n Enter the %d string”, i);

gets(s[i]);

}

Page 5: String Array (Multidimensional Arrays)

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]);

}

Page 6: String Array (Multidimensional Arrays)

(or)

char s[2][40] = {“I love ice cream”,”I love Jamaica”};

int i;

for(i=0; i<2; i++)

{

puts(s[i]);

}

Page 7: String Array (Multidimensional Arrays)

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:

Page 8: String Array (Multidimensional Arrays)

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.

Page 9: String Array (Multidimensional Arrays)

For ex: strcpy(s,”Welcome to Jamaica”);

strcpy(s, s1);

4.3 strncpy()

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);

Page 10: String Array (Multidimensional Arrays)

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.5 strncat()

This function concatenates two strings, but it lets you specify how many characters to concatenate. The syntax is:

strncat(char * str1, char * str2, size);

Page 11: String Array (Multidimensional Arrays)

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);

Page 12: String Array (Multidimensional Arrays)

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”);

Page 13: String Array (Multidimensional Arrays)

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.

Page 14: String Array (Multidimensional Arrays)

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.

Page 15: String Array (Multidimensional Arrays)

Ex:

char str[8] = “Jamaica”;

char *loc;

int pos;

loc = strchr(str, “a”);

pos = str - loc;

Page 16: String Array (Multidimensional Arrays)

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);

Page 17: String Array (Multidimensional Arrays)

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);

Page 18: String Array (Multidimensional Arrays)

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);

Page 19: String Array (Multidimensional Arrays)

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);

Page 20: String Array (Multidimensional Arrays)

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);

Page 21: String Array (Multidimensional Arrays)

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);

Page 22: String Array (Multidimensional Arrays)

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]);

Page 23: String Array (Multidimensional Arrays)

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])

{

}