Sie sind auf Seite 1von 21

COMS11500: Introduction to C

Julian Gough

November 11, 2013


Problem

Outline

1 Problem

2 Strings
Characters
Strings

3 ProblemInput

4 Arrays of Pointers
Pointer Arithmetic, Recap

5 ProblemOutput
Problem

Problem
Write a program that reverses short sentences

Please enter a sentence of at most 15 characters:


This is week 7!

The reverse sentence is:


7! week is This

Please enter a sentence of at most 15 characters:


This is week 7!?

The sentence is too long...


Strings

Outline

1 Problem

2 Strings
Characters
Strings

3 ProblemInput

4 Arrays of Pointers
Pointer Arithmetic, Recap

5 ProblemOutput
Strings Characters

Characters in C

The type char


C uses the type char to represent characters such as a, b, A, 1, etc.
C really treats a char as an 8-bit integer.
A signed char has a value between 128, . . . , 127
An unsigned char has a value between 0, . . . , 255
If you just write char, undefined which one is chosen!
Use single quotation marks for characters, e.g. char letter = a
Strings Characters

Characters in C

The type char


C uses the type char to represent characters such as a, b, A, 1, etc.
C really treats a char as an 8-bit integer.
Use single quotation marks for characters, e.g. char letter = a

Converting from lower to upper case


Small snippet that checks whether char ch is lower case and if so,
converts it to upper case (does not incorporate accents etc.)

if ((a <= ch) && ( ch <= z) {


ch = ch - a + A;
}

Better to use toupper from <ctype.h> library.


Strings Characters

Characters in C
Input/output
Using printf and scanf
The conversion specifier %c is used for char

char ch;
scanf("%c", &ch);
printf("%c", ch);

Using putchar and getchar


To write a single character, just write putchar(ch);
To read a single character, write ch = getchar();

Some idioms (source: K.N. King)


while (getchar() != \n); // skips rest of line
while ((ch = getchar()) == ); // skips blanks
Strings Strings

Strings in C
Strings are specially terminated arrays of char
An array of characters is known as a string.

char str1[] = {h, e, l, l, o, \0};


char str2[] = "hello";
char str3[6] = "hello";

Strings are terminated by the special \0 character.


The size of the array storing the string needs to be larger (by at least
one) than the length of the string.

char sentence[]="hello world!\n";


This string consists of 13 characters
But the array has size 14, since it needs to store \0 as well.
Strings Strings

Strings in C
Input/output

Using printf and scanf


The conversion specifier %s is used for strings

char blurb[20];
scanf("%19s", blurb); Pra meter slo 19 caracteres

printf("%s", blurb);
Strings Strings

Strings in C
Input/output

Using printf and scanf


The conversion specifier %s is used for strings

char blurb[20];
scanf("%19s", blurb);
printf("%s", blurb);

Safety first!
You need to specify the length of the blurb beforehand! Serious risk of
buer overflows without...

scanf("%s", blurb);
Strings Strings

Strings in C
Input/output

Using printf and scanf


The conversion specifier %s is used for strings

char blurb[20];
scanf("%19s", blurb);
printf("%s", blurb);

Attention on ampersands!
Correct: scanf("%19s", blurb);
Wrong: scanf("%19s", &blurb);
(Visual Studio allows bothwe dont!)
Strings Strings

Strings in C
Input/output

Using printf and scanf


The conversion specifier %s is used for strings

char blurb[20];
scanf("%19s", blurb);
printf("%s", blurb);

Using puts and gets


To write a string, just write puts(str);
To read a string, write ch = gets();
AVOID: Inherently unsafe with high risk of buer overflows...
Strings Strings

Strings in C
Input/output

Using printf and scanf


The conversion specifier %s is used for strings

char blurb[20];
scanf("%19s", blurb);
printf("%s", blurb);

Using fgets
fgets(blurb, sizeof(blurb), stdin);
This is always safe to read an entire line
It the input is too long, part of it is left unread;
If it fits, blurb will also contains the enter
We will see the meaning of stdin next week.
Strings Strings

Strings in C
Input/output

Overview of input methods


char blurb[20];
To scan a single word:
scanf("%19s", blurb);
No ampersand since blurb is eectively a pointer itself;
Specify how many characters to be read at most (for safety);
Do not use of Microsofts scanf_f (for portability).
To scan a single line:
fgets(blurb, sizeof(blurb), stdin);
Careful with enters
Strings Strings

Strings in C
The <strings.h> library

String manipulation made easy(ish)


By including <strings.h> you can do lots of stu quite easily
strlen returns the length of the string counted up to the (first) \0
null character (which itself is not counted).
strcpy and strncpy can be used to copy strings
strcat and strncat can be used to concatenate two strings. For
instance the concatenation of "hello " and "world!" is "hello
world!".
strcmp can be used to compare two strings based on lexicographic
ordering.
Strings Strings

Pointer Arithmetic
Plus the link with arrays

int a[6] = {5, 4, 3, 2, 1, 0};


int *aPtr;
int i;

for (i=0, aPtr=a; ((i < 6) && (*aPtr > 2)); i++, aPtr++) }
*aPtr += 10; // add 10 to content of this address
}

Pointers revisited
aPtr++ causes the pointer to advance
As a result, *aPtr == a[i]
In fact, a[i] is simply shorthand for *(a+i)
ProblemInput

Outline

1 Problem

2 Strings
Characters
Strings

3 ProblemInput

4 Arrays of Pointers
Pointer Arithmetic, Recap

5 ProblemOutput
Arrays of Pointers

Outline

1 Problem

2 Strings
Characters
Strings

3 ProblemInput

4 Arrays of Pointers
Pointer Arithmetic, Recap

5 ProblemOutput
Arrays of Pointers Pointer Arithmetic, Recap

Example of Pointer Arithmetic


One dimensional arrays

int a[5], b, *c;


a[3] = 7;
c = a; // Now c points to the same array as a
b = c[3];
c++; // Now c points to the next element, namely a[1]
*c = 6; // Now a[1] is assigned the value 6
Arrays of Pointers Pointer Arithmetic, Recap

Example of pointer arithmetic


Two-dimensional arrays

int a[2][3] = {{1, 2, 3},{4, 5, 6}}, b, *c, **d;


d = a; // now d points to the matrix a
c = a[0]; // now c points to the first row of a
b = c[1]; // now b will equal a[0][1] which is 2
ProblemOutput

Outline

1 Problem

2 Strings
Characters
Strings

3 ProblemInput

4 Arrays of Pointers
Pointer Arithmetic, Recap

5 ProblemOutput

Das könnte Ihnen auch gefallen