Reading:  

Getting started with C Language


String in C

Strings in C

Strings are the character arrays , or strings are array of characters. Any group of characters defined between double quotes are called strings in C. If we define any group of characters in between double quotes it is a constant string.

Example :

“Good  morning”

Here Good morning is the string (that is except double quotes). A string is stored in an array of characters. Each character in the string occupies one location in a character array. The null character ‘\0’ is put after the last character of that string. This null character is used to denote the end of the string. When   program see the null character it tell the compiler that here ends the string. The null character is also called a terminating character which is an important thing in a string. When  a string not terminated by ‘\0’ is not a string, can only considered as a collection of characters. A string may contain any characters, including special characters, like  space, \n, \t, \\ ...

Declaration of a string

As we know a string is a character array, we declare a string as an array of data type character.

Syntax :

char string-name[size];

example :

char name[10];

When the compiler assigns a character string to a character array , it automatically supplies a null character ( ‘\0’ ) at the end of that string. Thus the size of the string will be total number of characters plus one.

Initialization of a string

Initializing a string along with its declaration is possible. 

Example :

char name[7] = “Newton” ;

or

char name[7] = (‘N’,’e’,’w’,’t’,’o’,’n’,’\0’);

In the above example the string Newton has 6 character and a null character so the length of this string is 7. In the first example the null character is automatically supplied by the compiler. But in the second case that is by listing the elements of the array, we must supply the null character.

C support another method of initialization without specifying the size of the array.

Example :

char name[] = (‘T’,’o’,’m’,’a’,’s’,’\0’);

The compiler evaluates it as a 6 character length string.

Another method of initialization is :

char name[];
name[0] = ’N’;
name[1] = ’e’;
name[2] = ‘w’;
name[3] = ‘t’;
name[4] = ‘o’;
name[5] = ‘n’;

Reading the words

Reading a string is possible with the ‘scanf’ function. “%s” is used to read a character array.

 Example  :

char name[10];

scanf(“%s”,name);

The problem with scanf function is that it terminates its input on the first white space. A white space may be a tab, a space, a carriage return and the new line.

Printing the strings on screen

We can use printf function with ‘%s’ to print a string on to the screen. This printf function prints the string which is terminated by a null character.

Example :

printf(“%s”,name);

This will displays the contents of the  character array ‘name’. The header file #include<string.h> is used for string manipulations.

String handling functions

C support a large number of string handling functions that can be used to carry out many of string manipulations some of them are

strlen()         -  used to find the length of the string.

strcmp()      - used to compare two strings.

strcpy()        - used to copy one string to another.

strcat()         - used to combine two strings ( concatenation ) .

strstr()         - used for substring search.

strlen()

strlen function counts and returns the number of characters in a string.

Syntax :

Integer variable = strlen(sting name);

Example :

n=strlen(name);

where n is an integer variable which receives the value of the length of the string. The counting starts from the first character and ends at the null character.

strcmp()

The strcmp() function compares two strings identified by the arguments and has a value 0 if they are equal . if they are not it returns a numeric value 1.

Syntax :

 strcmp (string name1, string name2);

Example :

strcmp( name1,name2);

Our major concern is that whether the strings are equal.

strcpy()      

It works almost like a string assignment operator.

Syntax :

strcpy( string1, string2);

Example :

strcpy ( city, “Melbourne”);

it assigns the content of string2 to the string1.The string1 should be large in size than string2.

strcat ()  

strcat function joins two strings together.

Syntax :

strcat ( string1, string2 );

Example :

strcat( firstname,secondname) ;

gets(), puts()

These  two are similar to scanf() and printf() functions. gets() for reading the string and puts() for printing the string.

Syntax :

gets(string);

puts(string);

Example :

gets(name);

puts(name);

program to compare to strings using string function.

#include <stdio.h>
#include <string.h>
void main()
{
char a[10], b[10];
printf("Enter the first string");
gets(a);
printf("Enter the second string");
gets(b);
if( strcmp(a,b) == 0 )
printf("Entered strings are equal");
else
printf("Entered strings are not equal");
}

 

 

 

 

 

 

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!