Sie sind auf Seite 1von 38

Problem Solving & Problem Design Using C

AAPP005-3-2

C Character Arrays

Learning Outcomes
By the time you have completed this chapter, you will have acquired the ability to : Solve problems involving character data Use strings as a data type in problem solving

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 2 (of 38)

Key Terms you must be able to use


If you have mastered this topic, you should be able to use the following terms correctly in your assignments and exams: characters strings

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 3 (of 38)

Characters
A Character is any key you can strike on the
keyboard on the computer screen.

This includes upper and lowercase


letters,punctuation marks and even digits.

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 4 (of 38)

Characters
To store characters, we declare variables of
type char. For example : - char a;

Character literals can be assigned to char


variable using single quotes. For example : - char a = H; char initial = C; char C = s;
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 5 (of 38)

Strings
A string is a series of characters treated as
a single unit. A string may include letters, digits and various special characters such as +,-,*,/ and $. String literals or string constants are written in double quotation marks.

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 6 (of 38)

Strings

For example : - LOTUS DH0019 12345 Welcome to Malaysia

A string in C is an Array of characters


ending with the null character(\0).

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 7 (of 38)

Strings
For example, char color[]=White; or. char color[5]; scanf(%s,color); printf(%s,color); %s is a control string for the strings.

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 8 (of 38)

Lowercase into Uppercase


main() { char str1,res; printf(Enter a letter in lower case:);

scanf(%c,&str1);
if(str1 >= a && str1 < { res=str1-(a-A);
C Character String

z)

AAPP005-3-2 Problem Solving & Problem Design Using C

Slide 9 (of 38)

Lowercase into Uppercase


printf(\n UpperCase equivalent of %c is .%c\n,str1,res); }

else
printf(\nUppercase is.%c\n,str1); } Enter a letter in Lower Case: a

Upper Case equivalent of a is A


AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 10 (of 38)

Length of a String
#include<string.h>

main()
{ char str[50]; int len1=0; int i=0;

printf(\nEnter the String:);


gets(str);
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 11 (of 38)

Length of a String
while(str[i]<=\0) { } printf(\the length of the string %s is %d\n,str,len1);}
Enter the String : WELCOME OUTPUT: The length of the string WELCOME is 7
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 12 (of 38)

len1++; i++;

Copying a String
main() { char str1[20],str2[20]; int i=0; printf(\nEnter the String :);

gets(str1);
printf(\nBefore Copying%s\nstr1);
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String

Slide 13 (of 38)

Copying a String
while(str1[i] != \0)

{
}

str2[i]=str1[i];
i++;

printf(\nAfter Copying %s\n,str2); }


Enter the String : FRIDAY OUTPUT: Before Copying FRIDAY After CopyingFRIDAY
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 14 (of 38)

Comparing two Strings


main()

{
char str1[80],str2[80]; int i; printf(\nEnter the two strings:); gets(str1);

gets(str2);

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 15 (of 38)

Comparing two Strings


for(i=0;str1[i]==str2[i];i++)

if (str1[i] == \0)
printf(\n The Strings are Equal\n); else printf(\the Strings are not EqualThe numeric difference is %d\n,str1[i]-str2[i]); }

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 16 (of 38)

Comparing two Strings


Input: Enter the Two Strings: Hello

hello
Output:

Strings are Not Equal.


The numeric difference is -32

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 17 (of 38)

Reverse a String
main() { char str1[80]; int i=0; printf(\nEnter the String);

gets(str1);
while(str1[i] !=\0) i++; i--;
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 18 (of 38)

Reverse a String
while ( i >= 0)

{
printf(%c,str[i]); i--; } }
INPUT: Enter the String: LIVED OUTPUT: DEVIL
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 19 (of 38)

String Library Functions


C supports a wide range of string manipulation functions.The most common are,
Name Function

strcpy(s1,s2)
strcat(s1,s2)

Copies s2 into s1
Concatenates s2 onto the end of s1

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 20 (of 38)

String Library Functions


Name
strlen(s1) strcmp(s1,s2)

Function
Returns the length of s1. Returns 0 if s1 and s2 are the same;less than 0 if s1<s2;greater than 0 if s1>s2;

strrev(s1)

Reverses the string s1.

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 21 (of 38)

String Library Functions


EXAMPLE:

main()
{ char s1[30],s2[30]; gets(s1); gets(s2);

printf(\nLength of the String1:,strlen(s1);


printf(\nLength of the String2:,strlen(s2);
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 22 (of 38)

String Library Functions


if(!strcmp(s1,s2)) printf(\n The Strings are Equal\n); else printf(\the Strings are Not Equal\n); printf(\nConcatenation of the two strings :%s,strcat(s1,s2);

printf(\nAfter Copying: %s,strcpy(s1,This is Test);

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 23 (of 38)

String Library Functions


printf(\nReverse of s1 is%s\n,strrev(s1)); }
INPUT: Hello hello OUTPUT: Length of the string 1: 5 Length of the string 2: 5 The Strings are Not Equal Concatenation of the two strings :Hellohello After Copying : This is a test Reverse of the string .tset a si sihT
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 24 (of 38)

String Library Functions


Some of the other String library functions are,

Name
toupper(s1)

Function
Converts the s1 into Uppercase

tolower(s1)
trim(s1) ltrim(s1) rtrim(s1)
AAPP005-3-2 Problem Solving & Problem Design Using C

Converts the s1 into Lowercase


Removes the leading and the trailing blanks Removes the leading blanks Removes the trailing blanks
C Character String Slide 25 (of 38)

Alphabetical Order
main()
{ char a[5][10],temp[10];

int i,j;
printf(\nEnter the names one by one:\n); for(i=0;i<5;i++) scanf(%s,a[i]);
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 26 (of 38)

Alphabetical Order
for(i=0;i<5;i++) { for(j=i+1;j<5;j++) { if (strcmp(a[i],a[j]) > 0) { strcpy(temp,a[i]); strcpy(a[i],a[j]);

strcpy(a[j],temp);
} } }
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 27 (of 38)

Alphabetical Order
printf(\nAlphabetical Order\n); for(i=0;i<5;i++) printf(%s\n,a[i]); }

INPUT Enter the names one by one Sudha Pooja Aman Kavitha Sanjay
C Character String

OUTPUT Alphabetical Order Aman Kavitha Pooja Sanjay Sudha


Slide 28 (of 38)

AAPP005-3-2 Problem Solving & Problem Design Using C

Palindrome
A Palindrome is a string that is spelled the same way forwards and backwards. For Example, RADAR, MADAM, MALAYALAM. Example:

main()
{ char s[20],s1[20]; int i=0,j,length=0,t=0; printf(\nEnter the String:); gets(s);
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 29 (of 38)

Palindrom e
strcpy(s1,s);

length=strlen(s)-1;
while(i <= length) { if (s[i] != s1[length]) { printf(\nThe String is Not a Palindrome); t=1;

break;
}
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 30 (of 38)

Palindrom e
else

{
}

i=i-1;
length=length-1;

if (t==0) printf(\n The String %s is a Palindrome);

Enter the String: LOTUS The String is Not a Palindrome


C Character String Slide 31 (of 38)

AAPP005-3-2 Problem Solving & Problem Design Using C

Summary
Characters are the fundamental building

blocks of source programs. A string is a series of characters treated as a single unit. Function gets, reads characters from the keyboard input until a newline character is encountered. Function puts, takes a string as an argument and string followed by a newline character.

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 32 (of 38)

Summary
Function getchar reads a single character from
the keyboard. Function putchar prints its character argument. String always ends with the null character \0. The string predefined functions,such as strcpy() to copy the string,stcmp() to do the comparison between the two strings,strrev() to reverse the string, strlen() to find out the length of the string.

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 33 (of 38)

Quick Review Question


1. Write a program to find the length of a string. Write a program to count the number of vowels in the string.

2.

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 34 (of 38)

Follow Up Assignment

Read Chapter 8 from the text book. C: How to program Deitel/Deitel Answer the questions that follow the chapters.

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 35 (of 38)

Summary of Main Teaching Points

In this chapter you learnt about: Character data Strings String manipulation

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 36 (of 38)

Question and Answer Session

Q&A
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 37 (of 38)

Next Session

C Pointers

AAPP005-3-2 Problem Solving & Problem Design Using C

C Character String

Slide 38 (of 38)

Das könnte Ihnen auch gefallen