Professional Documents
Culture Documents
Strings
Strings
Strings
Strings :
• String is a set of characters.
• It is one dimensional array type of char.
• Every string terminated by null character ‘\0’ whose integer value
equivalent to zero.
char name[5][10] = {
"tree",
"bowl",
"hat",
"mice",
"toon"
};
Program to search for a string in the string array
/*process for finding the item in the string array*/
#include <stdio.h> for (i = 0; i < 5; i++) {
#include <string.h> x = strcmp(&name[i][0],
int main() { item); // compares the string in the array with
char name[5][10], the item and if
item[10]; // declaring the string array and the character // match is found returns 0 and stores it in
// array that will contain the string to be matched variable x
if (x == 0)
int i, x, f = 0; f = i;
}
/*taking 5 string inputs*/
printf("Enter 5 strings:\n"); /*if match is not found*/
for (i = 0; i < 5; i++) if (f == 0)
scanf("%s", &name[i][0]); printf("the item does not match any name in the list");
/*If the match is found*/
else
/*entering the item to be found in the string array*/ printf("Item Found. Item in the array exists at index -
printf("Enter the string to be searched:\n"); %d", f);
scanf("%s", &item); return 0;
}
String handling Functions:
strcpy() strcpy(string1, string2) Copies string2 value into string1
strncpy() strncpy(string1, string2, 5) Copies first 5 characters string2 into string1
strcmp() strcmp(string1, string2) Returns 0 if string1 and string2 are the same;
less than 0 if string1<string2; greater than 0 if
string1>string2