Computer Programming Basics
Computer Programming Basics
Basics
LECTURE 9
Lecture 9: Outline
\a Audible alert
\b Backspace
\f Form feed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\\ Backslash
\" Double quotation mark
\' Single quotation mark
\? Question mark
\nnn Octal character value nnn
\unnnn Universal character name
\Unnnnnnnn Universal character
name
\xnn Hexadecimal character value nn
Examples: Escape characters
Displaying a string: %s
printf ("%s\n", s1);
Inputting character strings
char string[80];
scanf ("%s", string); //NOT recommended !
The scanf function can be used with the %s format characters
to read in a string of characters up to a blank space, tab
character, or the end of the line, whichever occurs first
The string terminator (null character) is appended to string
If you type in more than 80 consecutive characters to the
preceding program without pressing the spacebar, the tab key,
or the Enter (or Return) key, scanf overflows the character
array !
Correct use of scanf for reading strings:
scanf ("%80s", string); // Safer version !
If you place a number after the % in the scanf format string,
Example: string processing
#include <stdio.h>
void concat (char result[], const char str1[], const char str2[]);
void main ()
{
const char s1[] = "Test " ;
const char s2[] = "works." ;
char s3[20];
concat (s3, s1, s2);
printf ("%s\n", s3);
}
Example: string processing
// Function to concatenate two character strings
void concat (char result[], const char str1[], const char str2[])
{
int i, j;
// copy str1 to result
for ( i = 0; str1[i] != '\0'; ++i )
result[i] = str1[i];
// copy str2 to result
for ( j = 0; str2[j] != '\0'; ++j )
result[i + j] = str2[j];
// Terminate the concatenated string with a null character
result [i + j] = '\0';
}
Testing strings for equality
void main ()
{
const char s1[] = "Test " ;
const char s2[] = "works." ;
int ok;
ok=compareStrings(s1, s2);
if (ok==1) printf("s1>s2");
else if (ok==-1) printf("s2>s1");
else printf("s2=s1");
}
Copying strings
strchr (s, c)
Searches the string s for the last occurrence of the character c. If
found, a pointer to the character in s is returned; otherwise, the null
pointer is returned.
strstr (s1, s2)
Searches the string s1 for the first occurrence of the string s2. If
found, a pointer to the start of where s2 is located inside s1 is
returned; otherwise, if s2 is not located inside s1, the null pointer is
returned.
getchar() and putchar()
/* Read characters from input over several lines until EOF. Count lines and
characters.*/
#include <stdio.h>
void main()
{
int nl, nc;
char c;
nl = 0;
nc = 0;
while ((c = getchar()) != EOF) {
nc++;
if (c == '\n')
nl++;
}
printf("Number of lines in input: %d\n", nl);
printf("Number of characters in input: %d\n", nc);
}
gets() and puts()
2. int a[3]={65,66,67};
printf("%c\n",a[0]);//A
printf("%c\n",a[1]);//B
printf("%c\n",a[2]);//C
ASCII character table
String to number
conversion functions
<stdlib.h>
atoi(s) converts string s to a type int value and returns it. The
function converts characters until it encounters something that is
not part of an integer.
atof() converts a string to a type double value and returns it
atol() converts a string to a type long value and returns it