/* * ======================================================================= * strings.c -- Exercise String and Character Functions * * The program design is as follows: * * Read a string that represents a file name prefix (e.g., "data") * Read in a string that represents a file name in the form prefix.N * Convert the file name to lower case * Find the . before N (beginning of string in the form .123) * Check that the file name actually starts with the prefix string * Copy and Convert N, still a char string, to an integer * Increment the integer form of N * Convert the integer N back into a string * Append the new incremented N to a copy of the file prefix * Print the new filename * * ======================================================================= * Copyright (C) 1998 by Mark Austin and David Mazzoni. * * This software is provided "as is" without express or implied warranty. * Permission is granted to use this software on any computer system, * and to redistribute it freely, subject to the following restrictions: * * 1. The authors are not responsible for the consequences of use of * this software, even if they arise from defects in the software. * 2. The origin of this software must not be misrepresented, either * by explicit claim or by omission. * 3. Altered versions must be plainly marked as such, and must not * be misrepresented as being the original software. * 4. This notice is to remain intact. * * Written by : David Mazzoni * ======================================================================= */ #include #include #include /* for tolower decl. */ #include /* for atoi decl. */ int main( void ) { enum { MaxString = 20 }; /* length of strings */ char caPrefix[ MaxString ]; char caFilename[ MaxString ]; char* cpToSuffix; char caSuffix[ MaxString ]; char caNewCopy[ MaxString ]; int iCount, iSuffix; /* [a] : Read a string that represents a file name */ /* prefix (e.g., "data") */ printf( "\n" ); printf( "Enter the lower case prefix for the " ); printf( "data file names (e.g., \"data\"):" ); scanf( "%s%*c", caPrefix ); /* [b] : Read in a string that represents a file name */ /* in the form prefix N. */ printf( "Enter the filename (e.g., \"data.122\"):" ); scanf( "%s%*c", caFilename ); /* [c] : Convert the file name to lower case */ for( iCount = 0; iCount < strlen(caFilename); iCount++ ) caFilename[iCount] = tolower(caFilename[iCount] ); /* [d] : Find the . before N (beginning of string in the form .123) */ cpToSuffix = strchr( caFilename, '.' ); /* [e] : 0 terminate prefix, save pointer to suffix */ *cpToSuffix++ = 0; /* [f] : Check that the file name actually starts with */ /* the prefix string. */ strcpy( caNewCopy, caFilename ); if( strcmp( caNewCopy, caPrefix ) ! 0 ) { printf( "\nImproper suffix error. Aborting.\n" ); return 1; } /* [g] : Copy and Convert N, still a char string, to an integer */ strcpy( caSuffix, cpToSuffix ); iSuffix = atoi( caSuffix ); /* [h] : Increment the integer form of N */ iSuffix += 1; /* [i] : Convert the integer N back into a string */ sprintf( caSuffix, "%i", iSuffix ); /* [j] : Append the new incremented N to a copy of the file prefix */ strcat( caNewCopy, "." ); strcat( caNewCopy, caSuffix ); /* [k] : Print the new filename */ printf( "\nThe new filename is %s\n", caNewCopy ); return 0; }