Cs Arrays
Cs Arrays
Faculty:
Mrs.Darshana A Naik
Mrs.Shilpa H
Mrs Sunitha R S
The above example represents string variables with an array size of 15. This means that the given character array is
capable of holding 15 characters at most. The indexing of array begins from 0 hence it will store characters from a
0-14 position. The C compiler automatically adds a NULL character '\0' to the character array created.
Initialization of a string variable. Following example demonstrates the initialization of a string variable,
'C' also allows us to initialize a string variable without defining the size of the character array. It can be done in
the following way,
The problem with the scanf function is that it never In order to read a string contains spaces, we use the
reads an entire string. It will halt the reading process gets() function. Gets ignores the whitespaces. It stops
as soon as whitespace, form feed, vertical tab, newline reading when a newline is reached (the Enter key is
or a carriage return occurs. pressed).
String Output: Print/Writing a String
•The standard printf function is used for printing or displaying a string on an output device. The format specifier
used is %s
•The puts function prints the string on an output device and moves the cursor back to the first position.
printf("%s", name);
char name[15];
•The function sizeof() is a unary operator in C language and used to get the size of any type of data in bytes.
Function Purpose
strlen() This function is used for finding a length of a string. It returns how many characters
are present in a string excluding the NULL character.
strcat(str1, str2) This function is used for combining two strings together to form a single string. It
Appends or concatenates str2 to the end of str1 and returns a pointer to str1.
strcmp(str1, str2) This function is used to compare two strings with each other. It returns 0 if str1 is
equal to str2, less than 0 if str1 < str2, and greater than 0 if str1 > str2.
Sr.No. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ;
}
//string comparison
val= strcmp(string1,string2);
if(val==0)
#include <string.h> {
printf("Strings are equal\n");
char string1[15]="Hello"; }
char string2[15]=" World!"; else
{
char string3[15]; printf("Strings are not equal\n");
int val; }
//string copy
//string concatenation
/
•strncpy(str1, str2, n) : This function is used to copy a string from another string. Copies the first n characters
of str2 to str1
•strstr(str1, str2): it returns a pointer to the first occurrence of str2 in str1, or NULL if str2 not found.
•strncat(str1, str2, n) : Appends (concatenates) first n characters of str2 to the end of str1 and returns a
pointer to str1.
Converting a String to a Number
•int atoi(str) Stands for ASCII to integer; it converts str to the equivalent int value. 0 is returned if the first
character is not a number or no numbers are encountered.
char *string_id[10];
int ID;
ID = atoi(string_id);