Chapter 13

 

 Strings

 

A string is a sequence of characters. We have used strings in a number of examples in the previous chapter. Any group of characters (Except double quote sign) defined between double quotations marks is a string constant. A string constant is used as the first parameter in a call to a printf () function.

 

Declaration and initializing string variables

 

C does not have string data type. A string is declared like an array of characters. The general form of declaration of a string variable is

 

char string_name[size];

 

The size determines the number of characters in the string_name. Some examples are:

            char name[20];

     char city[10];

 

When the compiler assigns a character string to a character array, it automatically appends a null character(‘\0’) at the end of the string. Therefore, the size of the string should be equal to the maximum number of characters in the string plus one.

 

Like numeric arrays, character arrays can also be initialized when they are declared. C permits a character array to be initialized in either of the following two forms:

a. char city[10]=”Kathmandu”;

b. char city[10]={‘k’,’a’,’t’,’h’,’m’,’a’,’n’,’d’,’u’};

 

 

C also permits us to initialize a character array without specifying the number of elements. In such cases, the size of the array will be determined automatically, based on the number of elements initialized. For example char city[]=”kathmandu”; Now let us write a simple program.

 

// program to illustrate difference between character array and string

#include<stdio.h>

#include<conio.h>

void main()

{

      char city1[10]=”kathamndu”;

      char city2[10]={ ‘k’,’a’,’t’,’h’,’m’,’a’,’n’,’d’,’u’};

      char city3[]=”kathmandu”;

printf(“%s”,city1);

printf(“%s”,city2);

printf(“%s”,city3);

}

 

Output:

kathmandu

kathmandu

kathmandu

In the introduction section of this chapter, we have seen that the printf() statement only requires the address of the first character of character array to print the string, This concept is demonstrated in the following program.

 

// program to illustrate the use of pointer and string

 

#include<stdio.h>

#include<conio.h>

void main()

{

      char city1[10]=”kathamndu”;

      char city2[10]={ ‘k’,’a’,’t’,’h’,’m’,’a’,’n’,’d’,’u’};

      char city3[]=”kathmandu”;

char *ptrcity1=city1;

char *ptrcity2=city2;

char *ptrcity3=city3;

 

printf(“%s\n”,ptrcity1);

printf(“%s\n”,ptrcity2);

printf(“%s\n”,ptrcity3);

}

 

Output:

kathmandu

kathmandu

kathmandu

 

In the program , we have assigned the addresses of the character array, city1,city2 and city3 to the pointer to char variable ptrcity1, ptrcity2 and ptrcity3 respectively as a result prtcity1, ptrcity2 and ptrcity3 hold addresses of the first element of the array i.e. k. when these addresses are supplied as arguments to printf() statement, it printed the strings so, printf statement needs only the first address of character array.

 

 

 

//Reading string from terminal

 

#include<stdio.h>

void main()

{

char name[20];

printf(“Enter your full name\n”);

scanf(“%s”,name);

printf(“Your name is :%s”,name);

}

 

Output:

 

Enter your full name

Abc xyz

Your name is: Abc

 

Another and more convenient method of reading string of text containing white spaces is to use the built in function gets().

 

// program to illustrate the use of gets() and puts() functions

#include<stdio.h>

#include<conio.h>

void main()

{

char name[20];

printf(“Enter your full name\n”);

gets(name);

puts(name);

getch();

}

 

Output:

 

Enter your full name\n”);

Abc xyz

Abc xyz

 

 

 

 

 

 

getch(),getche() and getchar() functions

 

These are called unformatted console input output functions. They are used to receive single character from input console. So far, we have consistently used the scanf() function to read a value from input console. But, the drawback of the scanf() function is to read a value from input console. But, the drawback of the scanf() function is that we must hit ENTER key before it accepts what we have typed. However, we often want a function that will read a single character as it is typed without waiting for the ENTER key to be hit. The getch() and getche() are two functions which serve this purpose. These functions return the character that has been most recently typed. The ‘e’ in the getche() function means it echoes(displays) the character that you type on the screen. The getch() function returns the character that you typed without echoing it on the screen. getchar() works in similar fashion and echoes the character you typed on the screen but requires ENTER key to be hit following the character that you typed.

 

#include<stdio.h>

#include<conio.h>

void main()

{

char ch;

printf("Press any key to continue\n");

getch();                // will not echo the character

printf("\n Type any character\n");

ch=getche();            //will echo the character typed

printf("\n Type any character \n");

getchar();              //will echo character, must followed by ENTER key

printf("\n Continue(y/n)");

getch();

}

Output:

Press any key to continue

 

Type any character

a

Type any character

g

Continue(y/n)

 

 

 

 

 

// program to print ASCII values of alphabet

#include<stdio.h>

#include<conio.h>

void main()

{

char ch;

int i=1;

clrscr();

for(ch=65;ch<=122;ch++)

{

if(ch>90 && ch<97)

continue;

printf("%c:%4d\n",ch,ch);

}

getch();

 

}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

String handling functions:-

 

Large number of strings handling functions can be carried out for string manipulations. Some of the most commonly used string handling functions are:

 

  1. strlen() function

 

            This function returns the length of a given string. It finds the length of the string.

 

// program that shows use of strlen() function

#include<stdio.h>

#include<conio.h>

 

void main()

{

int ln;

char str[]=”Kathmandu”;

printf(“String length using library function:”);

ln =strlen(str);

printf(“%d\n”, ln);

}

 

 

  1. strcpy() function

 

            This function copies the contents of one string to another

e.g

int length;

char str[]=”Kathmandu”;

char *sptr;

strcpy (sptr, str);

printf(“String is copied: %s\n”, sptr);

}

 

 

3. strcat() function-

 

This function concatenates two strings resulting in a single string. It takes two arguments, which are pointer to the two strings. The resultant string is stored in the first string specified in the argument list.

 

 

e.g

{

char str1[]=”Hello”;

char str2[]=”Kathamndu”;

strcat(str1,str2);

printf(“After concatenation:%s \n”,str1);

}

 

 

4. strcmp() function:

 

This function compares two strings. This function returns an integer determined by the relationship between the strings given in its arguments. The integer value is 0 if both strings are equal, positive if first string is greater than second string, negative if the first string is less than second string. The comparison takes place on the basis of their ASCII values.

 

// program to illustrate the use of strcmp() function.

 

#include<stdio.h>

#include<string.h>

#include<conio.h>

 

void main()

{

char str1[]=”ramesh”,str2[]=”ramensh”,str3[]=”rajesh”;

int check1,check2;

check1=strcmp(str1,str2);

check2=strcmp(str1,str3);

printf(“value of check1:%d”,check1);

printf(“value of check2:%d”,check2);

}

 

Output:

value of check1:0

value of check2:3

 

 

5. strupr() function

 

This function converts a lower case string to upper case. It takes one argument, which is pointer to first element of the string.

 

e.g

int length, ln;

static char str[]=”Kathmandu”;

printf (“Conversion is %s”, strupr(str));

 

 

6. strlwr() function

 

This function converts an upper case string to lower case. It takes one argument, which is pointer to the first element of the string.

 

e.g.

int length;

static char str [] =”KATHMANDU”;

printf(“Conversion is %s”, strlwr(str));

}

  Full Width

 

 


Post a Comment

0 Comments