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

An introduction to character strings and their manipulation in c programming language. It covers the declaration and initialization of character strings, the use of string i/o library routines, and string handling library routines such as copying, concatenating, finding length, and comparing strings. The document also explains how to pass an array to a function and sorts strings using an array of pointers.

Typology: Slides

2012/2013

Uploaded on 05/06/2013

apsara
apsara 🇮🇳

4.5

(2)

86 documents

1 / 23

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Strings
Lecture 16
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17

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 \