| |||
String compare and copy
Character Strings example from a Well House Consultants training course
More on Character Strings [link]
Source code: s1.c Module: C206
/* Strings in C are pointers to arrays of characters, null (\0) terminated.
Which means that if you compare them with the == operator, you're going to be comparing addresses, and not whether the strings contain the same text. And even if you put a "*" in front of the variable you're comparing, all you'll do is compare the first character. Solution is to use standard functions such as strcmp and strcpy - string compare and string copy. Both of these are regarded as dangerous as they can run off the end of stings that aren't properly null terminated, so you also have strncmp and strncpy which are better regarded, and have an extra parameter which is the maximum number of characters to compare / copy. Similarly, fgets to read in a string (use stdin for the keyboard) with a maximum length is much safer that gets which can easiiy be overrun by a too-long input! */ #include <stdio.h> #include <strings.h> int main() { char saying[81]; char host[81]; int i; for (i=0; i<2; i++) { printf("Whats yaname: "); fgets(saying,80,stdin); /* add a "null" in the last place to replace the new line that fgets adds (well - the user adds to indicate his response is complete! ) */ saying[strlen(saying)-1] = '\0'; printf("You said %s to me\n",saying); if (! strncmp(saying,"Graham",80)) { printf("It's your party\n"); } else { printf("Don't forget the booze\n"); } if (i == 0) strncpy(host,saying,80); } printf("Party %s and %s\n",host,saying); } /* Sample Output WomanWithCat:harwell grahamellis$ ./s1 Whats yaname: Graham You said Graham to me It's your party Whats yaname: Robert You said Robert to me Don't forget the booze Party Graham and Robert WomanWithCat:harwell grahamellis$ */ Learn about this subject
This module and example are covered on the following public courses:
* Learning to Program in C * Learning to program in C and C++ * Programming in C * C and C++ Programming * Learning to program in C and C++ * C and C++ Programming Also available on on site courses for larger groups Books covering this topic
Yes. We have over 700 books in our library. Books
covering C and C++ are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Character Strings" training module. You'll find a description of the topic and some
other closely related examples on the "Character Strings" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Web site author
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. | |||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |