Transcript Document

CS1061 C Programming
Lecture 15: More on Characters and Strings
A. O’Riordan, 2004
Text parsing

C has routines to perform useful tests and manipulations of character data
Each function receives a character or EOF(-1) as an argument

Need to include header file ctype.h

Following is a program that analyses a line of text for its constituent words:
- variable nwords (the return value from get_num_words) is the number of
words which the function finds.
- variable line is the text we're breaking into words
- for example, the string “quick brown fox” has 3 words.
Text parsing (2)
#include <stdio.h>
#include <stddef.h>
#include <ctype.h>
int get_num_words(char *);
int main(){
int num_words;
char *line = "This is the line";
num_words = get_num_words(line);
printf("There are %d words in total\n", num_words);
}
Text parsing (3)
int get_num_words(char *line)
{
char *p = line;
int nwords = 0;
while(1){
while(isspace(*p))
p++;
if(*p == '\0')
return nwords;
while(!isspace(*p) && *p != '\0')
p++;
nwords++;
if(*p == '\0')
return nwords;
}
}
Character Processing
Prototype
Description
int isdigit( int c )
Returns true if c is a digit and false otherwise.
int isalpha( int c )
Returns true if c is a letter and false otherwise.
int isalnum( int c )
Returns true if c is a digit or a letter and false otherwise.
int isxdigit( int c )
Returns true if c is a hexadecimal digit character and false otherwise.
int islower( int c )
Returns true if c is a lowercase letter and false otherwise.
int isupper( int c )
Returns true if c is an uppercase letter; false otherwise.
int tolower( int c )
If c is an uppercase letter, tolower returns c as a lowercase letter. Otherwise, tolower
returns the argument unchanged.
int toupper( int c )
If c is a lowercase letter, toupper returns c as an uppercase letter. Otherwise, toupper
returns the argument unchanged.
int isspace( int c )
Returns true if c is a white-space character—newline ('\n'), space (' '), form feed
('\f'), carriage return ('\r'), horizontal tab ('\t'), or vertical tab ('\v')—and
false otherwise
int iscntrl( int c )
Returns true if c is a control character and false otherwise.
int ispunct( int c )
Returns true if c is a printing character other than a space, a digit, or a letter and false
otherwise.
int isprint( int c )
Returns true value if c is a printing character including space (' ') and false
otherwise.
int isgraph( int c )
Returns true if c is a printing character other than space (' ') and false otherwise.
atoi() for string to integer


functions are available for converting strings of numerals into numeric types
and vice versa.
Need to include stdlib.h
The string "123" is not the same as the integer 123. When we have a string of
digits, we can convert it to the corresponding integer by calling the standard
routine atoi().
int i,j;
char string[] = "123";
i = atoi(string);
j = atoi("456");
itoa() for integer to string
/* convert an integer to a string */
#include <stdio.h>
#include <stdlib.h>
int main(){
int num;
char buff[20];
printf("Enter an integer: ");
scanf("%d", &num);
printf("As a string it is %s\n", itoa(num, buff, 10));
}
String Conversions
Prototype
double atof( const char *nPtr )
Description
Converts the string nPtr to double.
int atoi( const char *nPtr )
Converts the string nPtr to int.
long atol( const char *nPtr )
Converts the string nPtr to long int.
double strtod( const char *nPtr,
char **endPtr )
long strtol( const char *nPtr,
char **endPtr, int base )
unsigned long strtoul( const char
*nPtr, char **endPtr, int base )
Converts the string nPtr to double.
Converts the string nPtr to long.
Converts the string nPtr to unsigned
long.
A strcat() Example

The standard library function strcat concatenates strings - appends one string
onto the end of another, i.e it doesn’t create a new string.
Here's an example:
char string1[20] = "Hello, ";
char string2[] = "world!";
strcat(string1, string2);
printf("%s\n", string1);
Note: string1 here must be long enough to contain both string1 and string2
Command Line Arguments


giving a program some variable input to work on is by invoking it with
command line arguments.
C's model of the command line is that it consists of a sequence of words,
typically separated by whitespace. Your main program can receive these words
as an array of strings, one word per string.
All you have to do to receive it is to declare main as accepting two parameters:
int main(int argc, char *argv[]) {
...}
Variable argc is a count of the number of command-line arguments, and argv is an
array of the arguments themselves.
Command Line Arguments (2)
set of `words' making up the command line includes the name of program, argv[0]
points to the name of your program, argv[1] points to the first argument, etc.
This example simply prints its arguments and is called like this:
>prog first_arg second_arg
#include <stdio.h>
int main(int argc, char *argv[]){
int i;
for(i = 0; i < argc; i++)
printf("arg %d: %s\n", i, argv[i]);
}