1/17
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No study sessions yet.
library
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include < math.h>
check letter/ digit
isdigit () → digit
isalpha () → letter
isupper () → uppercase
islower () → lowercase
isalnum () → letter/ digit
→ not digit: !isdigit()
convert a digit character into its numeric value
( s[0] - ‘0’ )
scanf for string
for ( int i = 0 ; i < N ; i++ ) {
scanf ( "%d", &height[ i ] );
}
order from smallest to largest
for ( int i = 0 ; i < N ; i++ ) {
for ( int j = i + 1 ; j < N ; j++ ) {
if ( height[ i ] > height[ j ]) {
int temp = height[ i ];
height[ i ] = height[ j ];
height[ j ] = temp;
}
}
find min & max
int max = height[ 0 ];
int min = height[ 0 ];
int sum = height[ 0 ];
for ( int i = 1 ; i < N ; i++ ) {
if ( height[ i ] > max )
max = height[ i ];
else if ( height[ i ] < min )
min = height[ i ];
sum += height[ i ];
}
fgets in multidimensional array
for ( int i = 0 ; i < n ; i++ ) {
fgets ( name[ i ] , LEN , stdin );
name[ i ] [ strcspn ( name[ i ], "\n" ) ] = 0;
}
remove in string
char remove [ LEN ];
strcpy( remove, query + 7 ) ;
locate a substring in a string
if ( strstr ( address [ i ], remove ) != NULL )
element not null
s [ i ] != ‘ \0 ’
find frequency of number
int freq [ 100 ] = { 0 } ;
for ( int i = 0; s [ i ] != ' \0 '; i++) freq [ s [ i ] - ' 0 '] ++ ;
lowercase a string
void lowercase ( char s[ ] ) {
for ( int i = 0; s [ i ] ; i++) s[ i ] = tolower ( ( unsigned char ) s [ i ] );
};
compare two strings
strcmp(second1, "area") == 0
copy a string
strcpy ( min2 , name [ i ] ) ;
parse a log line
char line [ MAX ] ;
char id [ MAX ] [ LEN ] , plate [ MAX ] [ LEN ] , date [ MAX ] [ LEN ] , tim [ MAX ] [ LEN ] ;
int n = 0;
while ( fgets ( line , MAX , stdin )) {
line [ strcspn ( line, "\n" ) ] = 0 ;
if ( line [ 0 ] == ‘#’ ) break ;
// parse each log line
sscanf ( line , "%s %s %s %s", id [ n ] , plate [ n ] , date [ n ] , tim [ n ] ) ;
n++;
}
not count repetition
int count = 0;
char repeat [ MAX ] [ LEN ] ;
for ( int i = 0; i < n; i++ ) {
if ( strcmp ( findDate, date [ i ] ) == 0 ) {
int exist = 0;
for ( int j = 0; j < count; j++) {
if ( strcmp ( repeat [ j ] , plate [ i ] ) == 0) {
exist = 1;
break;
}
}
if (exist == 0) {
strcpy ( repeat [ count ], plate [ i ] );
count++;
}
}
}
eliminate input not letter or whitespace
int invalid ( char s [ ] ) {
for ( int i = 0; s [ i ] != '\0'; i++ ) {
if ( !isalpha ( ( unsigned ) s [ i ] ) && s [ i ] != ' ' ) return 1;
}
return 0;
} ;
find frequency of string
int count [ MAX ] = { 0 } ;
for ( int i = 0; i < m; i++ ) {
if ( count [ i ] ) continue;
int freq = 1;
for ( int j = i + 1; j < m; j++ ) {
if ( strcmp ( first [ i ], first [ j ]) == 0 ) {
freq ++;
count [ j ] = 1;
}
}
printf ("%s %d\n", first [ i ], freq) ;
}