110 likes | 260 Views
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
E N D
CS1061 C ProgrammingLecture 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; } }
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)); }
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 arrayof 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]); }