Lecture – Strings and Characers

Ryan Robucci

Table of Contents

http://www.csee.umbc.edu/courses/pub/www/courses/undergraduate/313/spring12/Lectures/CStrings.ppt

ASCII

char type

ASCII Character Chart

Special Characters

Special Char Example Code

printf(“\t\tMove over\n\nWorld, here I come\n");

        Move over

World, here I come

printf("I’ve written \”Hello World\”\n\t many times\n\a“);

I’ve written "Hello World"
    many times <beep>

Character Library

ctype.h

Character Input/Output

Array of char

Strings in C

String Output

Dangerous String Input

Safer String Input

C String Library

String Code

char first[10] = “bobby”;
char last[15] = “smith”;
char name[30];
char you[ ] = “bobo”;
strcpy( name, first );
strcat( name, last );
printf(%d, %s\n”, strlen(name), name );
strncpy( name, last, 2 );
printf(%d, %s\n”, strlen(name), name );
int result = strcmp( you, first );
result = strncmp( you, first, 3 );
strcat( first, last );

Simple Encryption

char c, msg[] = "this is a secret message";
int i = 0;
char code[26] = /* Initialize our encryption code */
{'t','f','h','x','q','j','e','m','u','p','i','d','c',
'k','v','b','a','o','l','r','z','w','g','n','s','y'} ;
/* Print the original phrase */
printf ("Original phrase: %s\n", msg);
/* Encrypt */
while( msg[i] != '\0){
  if( isalpha( msg[ i ] ) ) {
    c = tolower( msg[ i ] ) ;
    msg[ i ] = code[ c - ‘a’ ] ;
  }
  ++i;
}
printf("Encrypted: %s\n", msg ) ; 

Arrays of Strings

gets( ) to read a line

fgets( ) to read a line

fgets( )

#include <stdio.h>
#include <stdlib.h> /* exit */
int main ( )
{
double x ;
FILE *ifp ;
char myLine[42 ]; /* for terminating \0 */
ifp = fopen("test_data.dat", "r");
if (ifp == NULL) {
  printf ("Error opening test_data.dat\n");
  exit (-1);
}
fgets(myLine, 42, ifp ); /* read up to 41 chars*/
fclose(ifp); /* close the file when finished */
/* check to see what you read */
printf(”myLine = %s\n”, myLine);
return 0;
}

Detecting EOF with fgets( )

FILE *inFile;
inFile = fopen( “myfile”, “r” );
/* check that the file was opened */
char string[120];
while ( fgets(string, 120, inFile ) != NULL )
printf(%s\n”, string );
fclose( inFile );

Using fgets( ) instead of gets( )

“Big Enough”

What can happen?

#include <stdio.h>
int main( )
{
  char first[10] = "bobby";
  char last[15] = "smith";
  printf("first contains %d chars: %s\n", strlen(first), first);
  printf("last contains %d chars: %s\n", strlen(last), last);
  strcpy(first, "12345678901234567890"); /* too big */
  printf("first contains %d chars: %s\n", strlen(first), first);
  printf("last contains %d chars: %s\n", strlen(last), last);
  return 0;
}
gcc -fno-stack-protector  segfault.c 
./a.out 
first contains 5 chars: bobby
last contains 5 chars: smith
first contains 20 chars: 12345678901234567890
last contains 5 chars: smith
Segmentation fault (core dumped)

Summary

avoid gets

Excerpt fom Linux manpage for gets warns against its usage

man gets

reports:

Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because
gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer
security. Use fgets() instead.

Use length-limiting functions like fgets

fgets allows for a SIZE LIMIT to avoid buffer overruns.
It returns a pointer to the written array on success, or NULL if either EOF is detected or no character is read

char *fgets(char *s, int size, FILE *stream);