Information Technology Center

Home | About | Comments | Lectures | Notice | Registration

MASTERING C WITH MC KENZIE

Working With Strings Part I

  C Programming Main Page
Review Lesson Three
Lesson Four (b)

This lesson will introduce the use of handling strings with the scanf( ) function.

The scanf( ) function does not handle string data as smoothly as it does numeric data.

You Will Learn :


Strings

Before we can use a string variable we must declare it. A string is not a separate data type in the C language. It is really an array of the character data type. Another way to look at strings is to say it is a number of characters that are related to each other in some way. Arrays will be dealt with later.

If I want to get a user’s name into a variable called name then I will declare it as follows :

         char name[25];

This will allow the computer to set aside 25 bytes of memory space to hold the characters. We can now use up to 24 characters of the 25 we set aside. The last character space is reserved for what is called the NULL terminator which the compiler places at the end of each string.


Turbo C Source Code

#include<stdio.h>
void main()
{
   char name[25];

   printf("Please enter your full name : ");
  scanf("%s",name);
   printf("Hello %s",name);
  exit(0);
}

Instructions :

Did you observe that only you first name is printed and not your full name? This program stops reading characters as soon as it reaches a space.


To correct the problem we must modify the program telling the computer to read all characters until it reaches a code indicating that the enter key was pressed.

Modify your source code as follows :

#include<stdio.h>
void main()
{
   char name[25];

   printf("Please enter your full name : ");
  scanf("%[^\n]",name);
   printf("Hello %s",name);
  exit(0);
}

Explanation

  1.   The %s was replaced by [^\n] and that caused it to read all characters to the end of the line.

Using the gets( ) function

The gets( ) function is a neat function one can use to get string input. The format is :

   gets(variable name);

Here is a C program showing how to use gets( ) to get string input.

#include<stdio.h>
void main()
{
   char name[30];

   printf("Please enter your full name : ");
  gets(name);
   printf(“I am pleased to meet you %s”,name);
  exit(0);
}


Using the strlen( ) function

We should include the string.h header flie whenever we are processig string data.

Here is a program showing how to use strlen( ) to count the number of characters in a string.

#include<stdio.h>
#include<conio.h>
#include<string.h>

void main()
{
   int namelength;
   char name[30];

   clrscr( );
   printf("Enter your name : ");
  gets(name);
   namelength = strlen(name);
   printf(“Your name %s has %d characters ”,name, namelength);
  exit(0);
}

 

.......End of Lesson Four Part I .......

 

 


Lecturer:

The Tutor
Do you have a question or a comment ?
tutordam@yahoo.com


Home | About | Comments | Lectures | Notice | Registration