Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Using Strings and Characters in C Programming

September 5, 2013 Posted by: GPA Views: 711 0 comments

Problem

Write simple program using string.

Solution

Strings are a fundamental ingredient in almost every application. A string is a series of characters. A string may include letters, digits and other characters such as +, -, *, / and $. String literals, or string constants, in C are written in double quotation marks as follows:

"Peter Johnson is 25 years old"

A string in C is an array of characters ending in the null character ('\0'). The definitions:

char name[]="Peter"; - this definition creates a 6-element array name containing the characters 'P', 'e', 't', 'e' , 'r' and '\0';

char *name = "Peter";- this definition creates pointer variable name that points to the string "Peter" somewhere in memory. In this program prompt the user to enter name and show it on screen.

Example:

   

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
   char* nameusingPointer;
   char  nameusingArray[10];
   printf("http://globalproganswer.com\n");
   printf("Enter name using pointer: ");
   scanf("%s",&nameusingPointer);
   printf("Enter name using array: ");
   scanf("%s",&nameusingArray);
   printf("\nName using pointer is: %s ",&nameusingPointer);
   printf("\nName using array is: %s ",nameusingArray);
   printf("\n");
   system("PAUSE");
  return 0;
}

Output

using strings and characters in c programming

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

Function strtok in C programming Check prime numbers in C Programming C Programming Union Linked Lists in C programming File Input/Output in C programming C Programming Enumeration

RSS-subscribe

New materials

Subscribe