BrainDump
  Home arrow BrainDump arrow Page 2 - C: Strings
Iron Speed
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    C: Strings - Storing Strings
    (Page 2 of 6 )

    In C, you store strings in arrays, with each letter of the string going into a separate element in the array. Let's say we want to store a name in an array. Let's say the name is really cool and regal...something like: James. Here is how we would create the array to store the value and then access the array to print it out:


    #include <stdio.h>


    int main ()

    {

    char name[10]="James";

    printf (name);

     

    return(0);

    }

    Here we create an array named “name” and store the string “James” inside of it. Then we use the printf() function to print out the stored value, resulting in:

      James

    Storing Strings from User Input

    In the following code we will ask the user to enter their first name and then print that name to the screen:


    #include <stdio.h>

    int main ()

    {

    char name[10];

    printf("Enter your first name ");

    gets_s (name);

    printf("Your name is %s", name);

    return(0);

    }

    This program creates an array called “name”. It then prints some text asking the user to input their first name. Then it uses the get_s() function to store the data the user enters into the name array. Lastly we print out the value inside name using the printf() function and the %s placeholder.

    Note that when assigning the amount of elements in the array, be sure to include one extra for the null terminator (o) that C inserts at the end.

    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

    - Multiple Service Contracts and Indigo
    - Cleaning Out Your Programs in XP
    - Handling Metadata with Indigo
    - Building Blocks for a WCF Service Web Site
    - Help! I Need Some Remote Assistance
    - Using Service Templates with Indigo
    - Windows XP Tips for Task Manager
    - Generating Clients and Services with Indigo
    - Vista SP1, A Review
    - Services and the WCF
    - VBScript: Final Date Functions
    - Creating Services with the WCF
    - The Resource View of the MFC
    - VBScript: More Fun with the Date Functions
    - Vista Price Cuts Dissected




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway