string.h manipulation
Document Sample


STRING MANIPULATION
Presented by:
Engr. Adrian S. Valmocina
INITIALIZATION OF STRING
char var_name[index]
Where:
Char = data type of the string
var_name = name of the string container to be
used
index = length/capacity of the string container
Ways to assign a value to a string container/variable
>> Direct assignment/ declaration
> char string[] = “String to be assigned”
>> Via user input
> gets(var_name)
GETS
gets ( char str )
>>Get string from stdin
>>Reads characters from stdin and stores them as a string into str until
a newline character ('\n') or the End-of-File is reached.
>>The ending newline character ('\n') is not included in the string.
>>A null character ('\0') is automatically appended after the last
character copied to str to signal the end of the C string.
>>Notice that gets does not behave exactly as fgets does with stdin as
argument: First, the ending newline character is not included with gets
while with fgets it is..
PRINTING A STRING ON THE SCREEN
>>printf (“%s”, str)
>>puts(str)
where:
str = string container/ variable
PUTS
char puts (char *string );
>>Write string to stdout
>>Writes the C string pointed by str to stdout and appends a
newline character ('\n').
>>The function begins copying from the address specified
(str) until it reaches the terminating null character ('\0'). This
final null-character is not copied to stdout.
Using fputs(str,stdout) instead, performs the same operation
as puts(str) but without appending the newline character at
the end.
STRING FUNCTIONS (STRING.H)
>> strcmp
>> strcat
>> strcpy
>> strlen
STRLEN
Declaration:
int strlen( char *string)
Where:
string to be tested
>>Strlen returns the length of a string in integer
value.
STRCPY
Declaration:
char *strcpy(char *destination, char *source);
Where:
*destination >> output string
*source>> the container of the string to be copied
>>Strcpy copies one string to another. The destination must be
large enough to accept the contents of the source string
STRCMP
Declaration:
int strcmp(char *string1, char *string2);
Where:
string1,string2 >> string containers to be
compared
>> Strcmp compares two strings and returns an integer
indicating the difference between the strings. If the
strings match, then the number returned is 0.
STRCAT
Declaration:
char *strcpy(char *destination, char *source);
Where:
destination >> the string container that will get the resulting
concatenation
source >> string to be appended at the end of *destination
>> Strcat combines two strings and returns a pointer to the
destination string. In order for this function to work (and not seg
fault), you must have enough room in the destination for both
strings.
STRDUP
Declaration:
char *strdup(*source);
Where:
source >> string to copied
>> strdup makes a duplicate of string source
>> string allocation length = strlen(source) + 1
STRLWR, STRUPR
Declaration:
char *strlwr(char *string);
char *strupr(char *string);
Where:
string>> string to be converted
>> Strlwr converts uppercase letters (A-Z) into lowercase letters
>> Strupr converts lowercase letters (a-z) into uppercase letters
STRCHR
Declaration:
char *strchr(char *string, char c);
Where:
string >> the string to be searched
c >> character to be searched from the string
>> Strchr scans a string in the forward direction, looking for a
specific character
>> strchr finds the first occurrence of the character c in the string
STRRCHR
Declaration:
char *strrchr(char *string, char c);
Where:
string >> the string to be searched
c >> character to be searched from the string
>> Strrchr scans a string in the reverse direction, looking for a
specific character
>> strrchr finds the last occurrence of the character c in the string
STRREV
Declaration:
char *strrev(char *string);
Where:
string >> string to be reversed
>> strrev changes the characters in a string to reverse order,
except the terminating null character.
>> strrev returns a pointer to the reversed string
STRSPN, STRCSPN
Declaration:
char *strspn(char *string1, char *string2);
char *strspn(char *string1, char *string2);
>> Strspn finds the initial segment of string1 that consists entirely
of characters from string2.
>> Strspn finds the initial segment of string1 that consists entirely
of characters NOT from string2.
STRSTR
Declaration:
char *strstr(char *string1, char *string2);
>> strstr scans string1 for the first occurrence of the substring
string2
>> on success, returns a pointer to the element in string1 where
string2 begins (points string2 in string1)
THANK YOU!
Presented by:
Engr. Adrian S. Valmocina
Get documents about "