LAB 8: CHARACTER AND STRING

Similar documents
C Strings and Pointers

So far we have considered only numeric processing, i.e. processing of numeric data represented

Simple C++ Programs. Engineering Problem Solving with C++, Etter/Ingber. Dev-C++ Dev-C++ Windows Friendly Exit. The C++ Programming Language

ASCII Encoding. The char Type. Manipulating Characters. Manipulating Characters

Number Representation

Chapter 4: Computer Codes

Variables, Constants, and Data Types

Introduction to Python

C Examples! Jennifer Rexford!

CS106A, Stanford Handout #38. Strings and Chars

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

C++ Input/Output: Streams

Pemrograman Dasar. Basic Elements Of Java

Install Java Development Kit (JDK) 1.8

PL / SQL Basics. Chapter 3

Programming Languages CIS 443

Lab Experience 17. Programming Language Translation

Information and Computer Science Department ICS 324 Database Systems Lab#11 SQL-Basic Query

Python Lists and Loops

03 - Lexical Analysis

Basics of I/O Streams and File I/O

PaymentNet Federal Card Solutions Cardholder FAQs

JavaScript: Control Statements I

Compiler Construction

Introduction to Java. CS 3: Computer Programming in Java

Stacks. Linear data structures

C Programming. for Embedded Microcontrollers. Warwick A. Smith. Postbus 11. Elektor International Media BV. 6114ZG Susteren The Netherlands

( F O R M E R LY M Y A C C O U N T ) Quick Reference Guide - Updated Sept 21, 2015

Interpreting areading Scaled Scores for Instruction

Programming Exercises

Text Processing In Java: Characters and Strings

Using C# for Graphics and GUIs Handout #2

Cyber Security Workshop Encryption Reference Manual

Repetition Using the End of File Condition

Programming the PIC18/ XC8 Using C- Coding Dr. Farahmand

Introduction to UNIX and SFTP

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

2.1 Data Collection Techniques

Lecture 5: Java Fundamentals III

Solution for Homework 2

Introduction to Java

CSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions

Personal and Small Business Login Guide

The use of binary codes to represent characters

Systems I: Computer Organization and Architecture

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Regular Expression Syntax

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 3: Input/Output

Pseudo code Tutorial and Exercises Teacher s Version

Bachelors of Computer Application Programming Principle & Algorithm (BCA-S102T)

C++ Language Tutorial

NetSupport DNA Configuration of Microsoft SQL Server Express

DEBT COLLECTION SYSTEM ACCOUNT SUBMISSION FILE

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Applies to Version 6 Release 5 X12.6 Application Control Structure

Electronic Questionnaires for Investigations Processing (e-qip)

account multiple solutions

Introduction to Lex. General Description Input file Output file How matching is done Regular expressions Local names Using Lex

The string of digits in the binary number system represents the quantity

LIC MANAGEMENT SYSTEM

Moving from CS 61A Scheme to CS 61B Java

VB.NET - STRINGS. By calling a formatting method to convert a value or object to its string representation

Informatica e Sistemi in Tempo Reale

Sample CSE8A midterm Multiple Choice (circle one)

Conditionals (with solutions)

Computer Science 2nd Year Solved Exercise. Programs 3/4/2013. Aumir Shabbir Pakistan School & College Muscat. Important. Chapter # 3.

Building Java Programs

Lexical analysis FORMAL LANGUAGES AND COMPILERS. Floriano Scioscia. Formal Languages and Compilers A.Y. 2015/2016

Parental Control Setup Guide

The Hexadecimal Number System and Memory Addressing

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

How To Access A Secure From The State Of Iceland

Module 816. File Management in C. M. Campbell 1993 Deakin University

Passing 1D arrays to functions.

LabVIEW Day 6: Saving Files and Making Sub vis

Illustration 1: Diagram of program function and data flow

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

Microsoft Dynamics GP. Extender User s Guide

KT-1 Key Chain Token. QUICK Reference. Copyright 2005 CRYPTOCard Corporation All Rights Reserved

Part 1 Foundations of object orientation

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Lex et Yacc, exemples introductifs

2010/9/19. Binary number system. Binary numbers. Outline. Binary to decimal

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

J a v a Quiz (Unit 3, Test 0 Practice)

Positional Numbering System

CS177 MIDTERM 2 PRACTICE EXAM SOLUTION. Name: Student ID:

Tutorial 6 GPS/Point Shapefile Creation

ECS 15. Strings, input

Searching your Archive in Outlook (Normal)

How To Create A Database For Employee Records In A Club Account In A Computer System In A Cell Phone Or Cell Phone With A Cell Computer (For A Cell)

Import Filter Editor User s Guide

Building Java Programs

Setting Up Database Security with Access 97

Chapter 2: Elements of Java

Introduction This document s purpose is to define Microsoft SQL server database design standards.

PROGRAMMING IN C PROGRAMMING IN C CONTENT AT A GLANCE

Complete the Payroll Transfer Form

Transcription:

LAB 8: CHARACTER AND STRING Exercise 1: Character manipulation functions The following program demonstrates the usage of (predefined) character manipulation functions. In order to use the following functions, you need to include ctype.h as one of your header files. Copy and paste the following program: char ch; printf("enter a character, any character including symbol: "); scanf("%c",&ch); /*copy paste the following code here, one by one and observe the result*/ Copy and paste the following character manipulation function and observe the result. a) /* this function returns true if ch is a lowercase letter */ if(islower(ch)) printf("%c is a lowercase letter\n", ch); b) /* this function returns true if ch is an uppercase letter */ if(isupper(ch)) printf("%c is a uppercase letter\n", ch); c) /* this function returns true if ch is an alphabet */ if(isalpha(ch)) printf("%c is an alphabetic character\n", ch); d) /* this function returns true if ch is a digit */ if(isdigit(ch)) printf("%c is a decimal digit\n", ch); e) /* this function returns true if ch is a digit OR a letter */ if(isalnum(ch)) printf("%c is a alphanumeric character\n", ch); f) /* this function returns true if ch is a hexadecimal digit */ if(isxdigit(ch)) printf("%c is a hexadecimal digit\n", ch); g) /* this function returns true if ch is a printing character */ /* other than space, a digit or a letter */ if(ispunct(ch)) printf("%c is a punctuation character\n", ch); 1

h) /* this function returns true if ch is a whitespace character */ /* newline, space (' '), carriage return, tab */ if(isspace(ch)) printf("%c is a whitespace character\n", ch); i) /* this function returns true if ch is a printing character */ /* other than space */ if(isgraph(ch)) printf("%c is a printing character other than space\n", ch); j) /* this function returns true if ch is a printing character */ /* including space */ if(isprint(ch)) printf("%c is a printing character including space\n", ch); k) /* this function returns true if ch is a control character */ if(iscntrl(ch)) printf("%c is a control character\n", ch); Exercise 2: Character conversion function tolower( ) and toupper( ) are two functions that we use to change the letter case. tolower( ) is used to change an uppercase letter to its lowercase representation. toupper( ) is used to change a lowercase letter to its uppercase representation. You need to include ctype.h as one of your header files. Part 1: conversion function Copy and paste the following program. Run the program with different input (upper or lowercase letter) and observe the result. char ch; printf("enter a character: "); scanf("%c",&ch); if(islower(ch)) printf("%c in its uppercase letter is %c\n",ch, toupper(ch)); else if (isupper(ch)) printf("%c in its lowercase letter is %c\n",ch, tolower(ch)); else printf("neither an uppercase letter or lowercase letter entered\n"); 2

Part 2: The practical usage of tolower and toupper Both tolower( ) and toupper( ) functions are particularly useful during input validation. Say for a program that would keep on looping as long as the users type in y (for yes). We could use tolower( ) function to force any uppercase letter Y being entered by the users so that the program will still run correctly eventhough the users type in uppercase letter instead of lowercase letter. char cont = 'y'; while (cont == 'y') printf("\nkeep on looping"); fflush(stdin); printf("continue? (y/n):"); cont = tolower(getchar()); /* convert the read input to lowercase */ /* and assign it to variable cont */ Exercise 3: String manipulation functions In order for you to use string manipulation functions, you need to include string.h as one of your header files. Copy the following string declaration: #include <string.h> char str1[50] = "Malaysia Gemilang"; char str2[ ] = "Cemerlang"; char str3[100] = '\0'; int count; char str4[100] ='\0'; /* copy and paste the statements here */ 3

Copy and paste the following string manipulation function and observe the result. a) strcpy(str1, str2); /* copy the content of str2 into str1 */ b) strncpy(str1, str2, 4); /* copy the first 4 characters of str2 to str1 */ c) strcat(str1, str2); /* append str2 to str1 */ d) puts(str3); strncat(str1, str2, 4); /* append the 1 st 4 characters of str2 to str1 */ e) count = strlen(str1); printf("\n%s has %d characters including the space\n", str1, count); f) printf("\n\nenter the same string as above:"); gets(str4); if (strcmp(str1, str4) == 0) printf("both string are equal\n"); else printf("both string are not equal\n"); EXERCISE 4 Write a program that will count the number of alphabets, digits and punctuations in a sentence given by the user. (Hint: Use isalpha(), isdigit(), ispunct() and a repetition structure.) Enter your sentence: my tel no is 12345678. Your sentence contains: Alphabets: 9 Digits: 8 Punctuations: 1 4

EXERCISE 5 Write a program that prompts the user to enter his/her first name and a phone number. Your function should construct a user ID code by taking the first letter of the first name, followed by the first three digits of the phone number. Display the user ID code. (Hint: Use strncpy() and strncat().) Enter your first name and a phone number: zurin 3137862 Your user ID code is z313. 5