BrainDump
  Home arrow BrainDump arrow Page 5 - C: Strings
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

C: Strings
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-03-03

    Table of Contents:
  • C: Strings
  • Storing Strings
  • String Operations
  • Strcat()
  • Strcmp()
  • Strcpy()

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C: Strings - Strcmp()


    (Page 5 of 6 )

    C can't technically compare a string to a string, as it only sees strings as ascii codes. However, there is a function in C called strcmp() that allows you to do so. The function takes two strings and compares the value, returning 0 if they are equal and a non zero number if they are not. To demonstrate, I will show you how to create a program to insult someone named James:


    #include <stdio.h>

    #include <string.h>

    int main ()

    {

    char insult[10];

    printf("Please enter your first name: ");

    scanf_s("%s", insult);

    if( strcmp (insult, "James") == 0) {

    printf( "James is a fat man's name.n");

    }

    return(0);

    }

    This program creates an array named insult. It then asks the user to enter their name and stores the value in the insult array. Next we use the strcmp() function to compare the value of insult to “James” and if the result of that comparison equals 0, then it prints some text. If not, nothing happens. Note that the strcmp() function is case sensitive. There are two ways, for this example at any rate, to overcome this. First, we can make the program so it insults anyone, no matter what their name is:


    #include <stdio.h>

    #include <string.h>

    int main ()

    {

    char insult[10];

    printf("Please enter your first name: ");

    scanf_s("%s", insult);

    if( strcmp (insult, insult) == 0) {

    printf( "%s is a fat man's name.n",insult);

    }

    return(0);

    }

    This program works similar to the prior one, except now it compares the value of the array insult to itself. So let's say the user entered in the name Guido; the result would be:

      Please enter your name: Guido

      Guido is a fat man's name.

    The other way to do it is to use our dear friend, the _strcmpi function, which works in the same manner, but allows for non-case sensitivity.


    #include <stdio.h>

    #include <string.h>

    int main ()

    {

    char insult[10];

    printf("Please enter your first name: ");

    scanf_s("%s", insult);

    if( _strcmpi (insult, "James") == 0) {

    printf( "James is a fat man's name.n");

    }

    return(0);

    }

    More BrainDump Articles
    More By James Payne


       · Thanks for stopping by to read my article on C Strings. Here we discuss how to store...
     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT