Docsity

Character Strings and String Manipulation in C, Slides of Computer Engineering and Programming

Partial preview of the text

Download Character Strings and String Manipulation in C and more Slides Computer Engineering and Programming in PDF only on Docsity!

Strings

Lecture 16

Character Strings

  • Up until now, we have dealt mostly with simple character variables. A variable of type char may hold a single character and is assigned a value using a pair of single quotes: - Example: char onechar = 'z' ;
  • On the other hand, character strings are arrays of simple characters with a special character inserted into the string at the very end. They are assigned values with a pair of double quotes: - Example: char arraychar[6] = "abcde" ;

Character Strings

  • Declaration and initialization:

char color [ ] = "scarlet" ; or char *colorPtr = "scarlet" ; or char color [8] = {'s', 'c', 'a', 'r', 'l', 'e', 't', '\0'} ;

  • NOTE: Allowance must always be made for the terminating null character.

String I/O Library Routines

  • #include <stdio.h>

/* The following are function prototypes for some of the String I/O and Handling Library Routines */

/* Input next character as an integer */ int getchar (void) ;

/* Input string into array s until newline */ char *gets (char *s) ;

String I/O Library Routines

  • #include <stdio.h> <notice!!! โ€“ from stdio lib !!!

/* To input a number of characters from a stream */ char *fgets (char *string1, int n, FILE stream) ; / where up to (n - 1) characters are accepted, including newline / / To output a string of characters to a stream */ char *fputs (char *string1, FILE stream) ; / where all the characters are placed into the I/O stream */

String Handling Library Routines

  • #include <string.h> < Notice !!! โ€“ from string lib!!! /* To copy string 2 into string 1 */ char *strcpy (char *string1, const char string2) ; / To append string 2 to the end of string 1 */ char *strcat (char *string1, const char string2) ; / where first character of string 2 replaces the null in string 1 / / To find out length of string 1 (# of characters) */ size_t strlen (const char string1) ; / where "size_t" is either an "unsigned int" or "unsigned long" */

Arrays, Strings, and Pointers

/* Passing an array to a function */ #include <stdio.h> #include <string.h> void mysub ( char [ ] ) ; int main ( ) { char name[20] = "Richard J. Freuler" ; mysub (name) ; return 0 ; }

Arrays, Strings, and Pointers

void mysub ( char text[ ] )

{

int len, k ; len = strlen (text) ; printf ("%d\n", len ) ; for (k = 0 ; k < len ; k++) printf ("%c", text [k] ) ;

}

/*Program Output */

18

blanks

Sorting Strings with Pointers

  • Now, C has to know how many characters are actually stored in each string. Recall that a string termination character, or null, ('\0') is inserted after the last character.
  • Note that a string termination character takes one array element to store, therefore the maximum string length that could be stored in one row of the array mystrings is n - 1.
  • Often, a string is stored with a newline Docsity.com

Sorting Strings with Pointers

  • The address of the beginning of the array mystrings is just the array name mystrings without any subscripts.
  • The address of any string (any row) in the array is mystrings[k] where k is the row number.
  • Since all subscripts start at zero in C, mystrings and mystrings[0] are effectivelyDocsity.com

Sorting Strings with Pointers

/* This program sorts strings using array of pointers */ #include <stdio.h> #include <string.h> #define SIZE 4 int main ( ) { int k, swaps ; char name[SIZE][30], *nptr[SIZE], *temp ; char filename[ ] = "namelist.dat" ; FILE *fptr ; fptr = fopen (filename,"r") ;

Sorting Strings with Pointers

printf ("\n Original list\n") ;

for (k = 0 ; k < SIZE ; k++) /* Assign addresses / { / to pointers in array / nptr[k] = name[k] ; fgets (name[k], 30, fptr) ; printf ("%s", name[k]) ; / Print original list */ }

Sorting Strings with Pointers

do /* Sort the pointers in ascending alphabetic / { / order of the strings */ swaps = 0 ; for (k = 0 ; k < (SIZE - 1) ; k++) { if (strcmp (nptr[k], nptr[k+1]) > 0) { temp = nptr[k] ; nptr[k] = nptr[k+1] ; nptr[k+1] = temp ; swaps=1 ; } } } while (swaps);

Sorting Strings with Pointers

0 1 2 3 4 5 โ€ฆ 29 0 1 2 3

So, the name array would still look like:

And, the nptr array would end up as:

260 230 290 200

0 1 2 3

M I R A N D A \n
J A S O N \n
A N D Y \n
M E G A N \n \