String Functions and Character Input/Output in C: A Comprehensive Guide, Study notes of Electrical and Electronics Engineering

An in-depth exploration of various string functions and character input/output operations in c programming language. It covers topics such as using %s with scanf and printf, char input functions, string manipulation functions, conversion functions, and character testing and conversion functions. It also includes examples of how to use these functions in your code.

Typology: Study notes

Pre 2010

Uploaded on 08/18/2009

koofers-user-c0x
koofers-user-c0x 🇺🇸

9 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
String Functions
Input (#include <stdio.h>)
(1)Using %s with scanf or fscanf: skips leading white space to first nonwhite space, begins converting stopping
with next white space; adds null. Addition of field width sets maximum number of characters to look at, will still
stop sooner if encounters white space: %7s will input no more than 7 characters.
(2)char * gets(char *string); reads until encounters new line or end of file. Does not store new line; does add
terminating null. Returns pointer to string stored.
(3)char * fgets(char * string,int n,FILE * ptr); reads no more than n characters from file pointed to by ptr, stops
with end of line and DOES store it, also adds null. Returns pointer to string stored.
Output(#include <stdio.h>)
(1)Using %s with printf or fprintf; outputs characters in string up to but not including null. Can use field width to
get extra blanks (normally right-justified, use –flag to left-justify); use precision to specify maximum number of
characters to output.
(2)int puts(const char *string); writes string up to null, then writes new line; returns positive integer if successful.
(3)int fputs(const char * string,FILE *ptr); write string, including new line if present, to file pointed to by ptr. Does
not follow with new line unless already in string.
Manipulation(#include <string.h>)
(1)size_t strlen(const str); returns number of characters in str up to but not including null.
(2) (a) char *strcpy(char *s1,const char *s2); copies the string s2 into the string s1, including the terminating
null. Programmer is responsible for enough room in s1. The value of s1 is returned.
(b) char *strncpy(char *s1,const char *s2,size_t n); same as strcpy except precisely n characters are written
into s1. The characters are taken from s2 until the null is reached or n characters have been taken, whichever
comes first. Any remaining characters in s1 are replaced by nulls.
(3) (a) char *strcat(char *s1,const char *s2); concatenates strings s1 and s2—that is, the string s2 is appended
to the end of s1 with the first character in s2 replacing the null of the original s1. Programmer is responsible for
enough room in s1. Returns the value of s1.
(b) char *strncat(char *s1,const char *s2,size_t n); same as strcat except at most n characters of s2, not
including the null, are appended, followed by the null.
(4) (a) int strcmp(const char *s1,const char *s2); compares strings s1 and s2 lexicographically. Returns a value
that is less than, equal to, or greater than 0 depending on whether s1 is lexicographically less than, equal to or
greater than s2.
(b) int strncmp(const char *s1,const char *s2,size_t n); same as strcmp except compares at most n
characters, stopping when nth characters are compared or a null is reached, whichever happens first.
(5) char *strstr(const char *s1,const char *s2); searches in s1 for the first occurence of the substring s2. If the
search is successful, a pointer to the base address of the substring in s1 is returned; otherwise a NULL pointer
is returned.
(6) char *strchr(const char *str,int c); searches in str for the first occurrence of the character c. If the search is
successful, the address of the character is returned; otherwise a NULL pointer is returned.
Conversion(#include <stdlib.h>)
All ato... functions behave similarly in that they are used to convert a string to a value. The string itself does not
change, the value (of the appropriate type is returned). The string can begin with white space, which is ignored.
The conversion stops at the first inappropriate character. If no conversion takes place, 0 is returned.
(1) double atof(const char *s); returns equivalent floating point number (as a double)
(2) int atoi(const char *s); returns equivalent integer number
pf2

Partial preview of the text

Download String Functions and Character Input/Output in C: A Comprehensive Guide and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

String Functions

Input (#include <stdio.h>)

(1)Using %s with scanf or fscanf: skips leading white space to first nonwhite space, begins converting stopping with next white space; adds null. Addition of field width sets maximum number of characters to look at, will still stop sooner if encounters white space: %7s will input no more than 7 characters. (2)char * gets(char *string); reads until encounters new line or end of file. Does not store new line; does add terminating null. Returns pointer to string stored. (3)char * fgets(char * string,int n,FILE * ptr); reads no more than n characters from file pointed to by ptr, stops with end of line and DOES store it, also adds null. Returns pointer to string stored.

Output(#include <stdio.h>)

(1)Using %s with printf or fprintf; outputs characters in string up to but not including null. Can use field width to get extra blanks (normally right-justified, use –flag to left-justify); use precision to specify maximum number of characters to output. (2)int puts(const char *string); writes string up to null, then writes new line; returns positive integer if successful. (3)int fputs(const char * string,FILE *ptr); write string, including new line if present, to file pointed to by ptr. Does not follow with new line unless already in string.

Manipulation(#include <string.h>)

(1)size_t strlen(const str); returns number of characters in str up to but not including null. (2) (a) char *strcpy(char *s1,const char *s2); copies the string s2 into the string s1, including the terminating null. Programmer is responsible for enough room in s1. The value of s1 is returned. (b) char *strncpy(char *s1,const char *s2,size_t n); same as strcpy except precisely n characters are written into s1. The characters are taken from s2 until the null is reached or n characters have been taken, whichever comes first. Any remaining characters in s1 are replaced by nulls. (3) (a) char *strcat(char *s1,const char *s2); concatenates strings s1 and s2—that is, the string s2 is appended to the end of s1 with the first character in s2 replacing the null of the original s1. Programmer is responsible for enough room in s1. Returns the value of s1. (b) char *strncat(char *s1,const char *s2,size_t n); same as strcat except at most n characters of s2, not including the null, are appended, followed by the null. (4) (a) int strcmp(const char *s1,const char *s2); compares strings s1 and s2 lexicographically. Returns a value that is less than, equal to, or greater than 0 depending on whether s1 is lexicographically less than, equal to or greater than s2. (b) int strncmp(const char *s1,const char *s2,size_t n); same as strcmp except compares at most n characters, stopping when nth characters are compared or a null is reached, whichever happens first. (5) char *strstr(const char *s1,const char *s2); searches in s1 for the first occurence of the substring s2. If the search is successful, a pointer to the base address of the substring in s1 is returned; otherwise a NULL pointer is returned. (6) char *strchr(const char *str,int c); searches in str for the first occurrence of the character c. If the search is successful, the address of the character is returned; otherwise a NULL pointer is returned.

Conversion(#include <stdlib.h>)

All ato... functions behave similarly in that they are used to convert a string to a value. The string itself does not change, the value (of the appropriate type is returned). The string can begin with white space, which is ignored. The conversion stops at the first inappropriate character. If no conversion takes place, 0 is returned. (1) double atof(const char *s); returns equivalent floating point number (as a double) (2) int atoi(const char *s); returns equivalent integer number

Character Functions

Input a single character (#include <stdio.h>)

int getchar(void ) – returns next character read from standard input or EOF if end of file; declared as int rather than char because value of EOF may not be in range allowed for char data type. int fgetc(FILE *) – returns next character read from file pointed to by argument or EOF if end of file or error. int getc(FILE *)—same as fgetc except usually implemented as macro.

Output a single character (#include <stdio.h>)

int putchar(int character ) – write single character argument to standard output (may not actually appear until new line is written due to buffering) Returns character written or EOF in case of error int fputc(int character ,FILE * fileptr)—writes character which is first argument to file pointed to by fileptr; returns the character written if successful or EOF in case of error. int putc(int character ,FILE *fileptr)—same as fputc except usually implemented as macro All of the following require #include <ctype.h>

Testing a character

All these functions take a single character argument and return a nonzero (true) value if the test is true and zero (false) if the test is false. int isalnum(int c); is alphanumeric? int isalpha(int c); is alphabetic? int iscntrl(int c); is control character? int isdigit(int c); is digit? int islower(int c); is lowercase letter? int ispunct(int c); is punctuation? int isspace(int c); is white space? int isupper(int c); is uppercase letter?

Converting a character

int tolower(int c); given an uppercase letter c , the equivalent lowercase letter is returned; otherwise c is returned int toupper(int c); given a lowercase letter c, the equivalent uppercase letter is returned; otherwise c is returned.