unit 3 (1) (1)
unit 3 (1) (1)
strncpy( )
The strncpy( ) is similar to the strcpy( )
It copies the given n numbers of character
from
another string
Example:
char symbols[100]= “abcdef@#&+-!” ;
char alpha[100];
strncpy(alpha , symbols, 6 ) ;
strchr( )
The strchr() function is used to find the fir
occurrence of a specified character in a
string.
It returns pointer to the first occurrence of
the specified character.
It returns NULL if the character is not
found.
Example:
strrchr( )
The strchr() function is used to find the las
occurrence of a specified character in a
string.
It returns pointer to the last occurrence of
the specified character.
It returns NULL if the character is not
found.
Example:
strstr( )
strstr ( ) searches for the first occurrence o
a
specified substring within a string.
It returns pointer to the first occurrence of
the specified substring.
It returns NULL if the substring is not
found.
Example:
char *ptr =strstr(“coldplay”, “play”);
The address of character ‘p’ (xf5) will be
stored
in ptr
atol(ASCII to Long)
atol converts a string to an long integer value
Example:
atol(“9876543210”);
Output: 9876543210
atof(ASCII to Float)
atof converts a string to an floating value
Example:
atol(“3.1415”);
Output: 3.1415
8. Explain the memory model of arrays in C.
How are arrays stored in memory ,how does
indexing work.
Arrays in memory are stored as contiguou
blocks of memory. The Elements are stor
one after the other in sequence.
Example :
// 1D (linear array)
char twinkle[ 5]={‘S’, ‘t’, ‘a’, ‘r’, ‘s’};
//2 D array
char mat_A[2][2]= {1 , 2 , 3 , 4};
Indexing in Arrays:
Indexing refers to process of accessing eleme
using its position/index.
Array index starts from zero.