Beruflich Dokumente
Kultur Dokumente
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
C Character String
C Character String
Characters
A Character is any key you can strike on the
keyboard on the computer screen.
C Character String
Characters
To store characters, we declare variables of
type char. For example : - char a;
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.
C Character String
Strings
C Character String
Strings
For example, char color[]=White; or. char color[5]; scanf(%s,color); printf(%s,color); %s is a control string for the strings.
C Character String
scanf(%c,&str1);
if(str1 >= a && str1 < { res=str1-(a-A);
C Character String
z)
else
printf(\nUppercase is.%c\n,str1); } Enter a letter in Lower Case: a
Length of a String
#include<string.h>
main()
{ char str[50]; int len1=0; int i=0;
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
Copying a String
while(str1[i] != \0)
{
}
str2[i]=str1[i];
i++;
{
char str1[80],str2[80]; int i; printf(\nEnter the two strings:); gets(str1);
gets(str2);
C Character String
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]); }
C Character String
hello
Output:
C Character String
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)
strcpy(s1,s2)
strcat(s1,s2)
Copies s2 into s1
Concatenates s2 onto the end of s1
C Character String
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)
C Character String
main()
{ char s1[30],s2[30]; gets(s1); gets(s2);
C Character String
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
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
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;
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.
C Character String
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.
C Character String
2.
C Character String
Follow Up Assignment
Read Chapter 8 from the text book. C: How to program Deitel/Deitel Answer the questions that follow the chapters.
C Character String
In this chapter you learnt about: Character data Strings String manipulation
C Character String
Q&A
AAPP005-3-2 Problem Solving & Problem Design Using C C Character String Slide 37 (of 38)
Next Session
C Pointers
C Character String