STRINGS
Subject :Computer Programming and Utilization(
Active Learning Assignment(ALA)
Strings
• String:- A sequence of characters.
• The ending character is always null character ‘0’.
• Strings are important in many programming.
Examples:-
“The cow jump over the moon.”
“123456” etc.
Declaration of Strings
• Syntax: char string_name[length];
• Example: char name[5];
• Since string is an array, the declaration of a string is the same as declaring a char array.
char string_var[30];
char string_var[20] = “Initial value”;
"A String" A 0gnirtS
Memory Storage for a String
• The string is always ended with a null
character ‘0’.
• The characters after the null character are
ignored.
• e.g., char str[20] = “Initial value”;
n i t i a l v a l u e ? ? …I 0
[0] [13]
Input/Output of a String
• The placeholder %s is used to represent string
arguments in printf and scanf.
– printf(“Topic: %sn”, string_var);
• The string can be right-justified by placing a
positive number in the placeholder.
– printf(“%8s”, str);
• The string can be left-justified by placing a
negative number in the placeholder.
– printf(“%-8s”, str);
Various Operations Possible on Strings
1. To find length of string
2. To copy content of one string to another string
3. To change the case of letters in the strings
4. To do encryption and decryption operation with string
5. To find out tokens from the string
6. To do some string manipulation and rearranging the
strings
7. To search match
Some String Functions from string.h
Function Purpose Example
strcpy Makes a copy of a
string
strcpy(s1, “Hi”);
strcat Appends a string to the
end of another string
strcat(s1, “more”);
strcmp Compare two strings
alphabetically
strcmp(s1, “Hu”);
strlen Returns the number of
characters in a string
strlen(“Hi”)
returns 2.
strrev Returns the reverse
string of string.
strrev(“Hello”);
Returns olleH.
Function strcat
• Functions strcat appends a copy of string s2 to the end of s1 and terminate s1 with null
and returns s1.
• Example:-
#include<stdio.h>
#include<string.h>
main()
{
char a[100],b[100];
printf("enter the 1st string");
scanf("%s",a);
printf("enter the 2nd string");
scanf("%s",b);
strcat(a,b);
printf("string obtained is %sn",a);
}
Output:-
enter the 1st string Hello
enter the 2nd string World
string obtained is Hello World
Function strcpy
• Function strcpy copies one string into another string.
• Example:-
#include<stdio.h>
#include<string.h>
main()
{
char s1[15],s2[15];
printf("enter s1");
scanf("[^n]",s1);
strcpy(s2,s1);
printf("n copied s2 is %s",s2);
}
• Output:-
enter s1 Hello
copied s2 is Hello
• Write a program to accept the string and find out its length using string.
#include<stdio.h>
#include<string.h>
void main()
{
char s[80];
int i,tn;
printf(“Enter a string:”);
scanf(“%s”,&s);
tn=strlen(s);
printf(“nLength of %s is %d”,s,tn);
}
Output:-
Enter a string:Computer
Length of Computer is 8
Distinction Between Characters and
Strings
• The representation of a char (e.g., ‘Q’) and a
string (e.g., “Q”) is essentially different.
–A string is an array of characters ended with
the null character.
Q
Character ‘Q’
Q 0
String “Q”
String Comparison (1/2)
• Suppose there are two strings, str1 and str2.
– The condition str1 < str2 compare the initial memory address
of str1 and of str2.
• The comparison between two strings is done by comparing each
corresponding character in them.
– The characters are comapared against the ASCII table.
– “thrill” < “throw” since ‘i’ < ‘o’;
– “joy” < joyous“;
• The standard string comparison uses the strcmp and strncmp
functions.
String Comparison (2/2)
Relationship Returned Value Example
str1 < str2 Negative “Hello”< “Hi”
str1 = str2 0 “Hi” = “Hi”
str1 > str2 Positive “Hi” > “Hello”
• e.g., we can check if two strings are the same by
if(strcmp(str1, str2) != 0)
printf(“The two strings are different!”);
Array of Strings
• Table of strings is called as Array of strings.
Example:
char name[3][10]={“Sharda”,”Neelima”,”Geeta”};
//(Array of strings)Two dimensional array of strings
• Name is an array of 3 strings;each containing 10 characters.The total storage
for name requires 30 bytes as below.
S h a r d a 0
N e e l i m a 0
G e e t a 0
Strings CPU GTU

Strings CPU GTU

  • 1.
    STRINGS Subject :Computer Programmingand Utilization( Active Learning Assignment(ALA)
  • 2.
    Strings • String:- Asequence of characters. • The ending character is always null character ‘0’. • Strings are important in many programming. Examples:- “The cow jump over the moon.” “123456” etc.
  • 3.
    Declaration of Strings •Syntax: char string_name[length]; • Example: char name[5]; • Since string is an array, the declaration of a string is the same as declaring a char array. char string_var[30]; char string_var[20] = “Initial value”; "A String" A 0gnirtS
  • 4.
    Memory Storage fora String • The string is always ended with a null character ‘0’. • The characters after the null character are ignored. • e.g., char str[20] = “Initial value”; n i t i a l v a l u e ? ? …I 0 [0] [13]
  • 5.
    Input/Output of aString • The placeholder %s is used to represent string arguments in printf and scanf. – printf(“Topic: %sn”, string_var); • The string can be right-justified by placing a positive number in the placeholder. – printf(“%8s”, str); • The string can be left-justified by placing a negative number in the placeholder. – printf(“%-8s”, str);
  • 6.
    Various Operations Possibleon Strings 1. To find length of string 2. To copy content of one string to another string 3. To change the case of letters in the strings 4. To do encryption and decryption operation with string 5. To find out tokens from the string 6. To do some string manipulation and rearranging the strings 7. To search match
  • 7.
    Some String Functionsfrom string.h Function Purpose Example strcpy Makes a copy of a string strcpy(s1, “Hi”); strcat Appends a string to the end of another string strcat(s1, “more”); strcmp Compare two strings alphabetically strcmp(s1, “Hu”); strlen Returns the number of characters in a string strlen(“Hi”) returns 2. strrev Returns the reverse string of string. strrev(“Hello”); Returns olleH.
  • 8.
    Function strcat • Functionsstrcat appends a copy of string s2 to the end of s1 and terminate s1 with null and returns s1. • Example:- #include<stdio.h> #include<string.h> main() { char a[100],b[100]; printf("enter the 1st string"); scanf("%s",a); printf("enter the 2nd string"); scanf("%s",b); strcat(a,b); printf("string obtained is %sn",a); } Output:- enter the 1st string Hello enter the 2nd string World string obtained is Hello World
  • 9.
    Function strcpy • Functionstrcpy copies one string into another string. • Example:- #include<stdio.h> #include<string.h> main() { char s1[15],s2[15]; printf("enter s1"); scanf("[^n]",s1); strcpy(s2,s1); printf("n copied s2 is %s",s2); } • Output:- enter s1 Hello copied s2 is Hello
  • 10.
    • Write aprogram to accept the string and find out its length using string. #include<stdio.h> #include<string.h> void main() { char s[80]; int i,tn; printf(“Enter a string:”); scanf(“%s”,&s); tn=strlen(s); printf(“nLength of %s is %d”,s,tn); } Output:- Enter a string:Computer Length of Computer is 8
  • 11.
    Distinction Between Charactersand Strings • The representation of a char (e.g., ‘Q’) and a string (e.g., “Q”) is essentially different. –A string is an array of characters ended with the null character. Q Character ‘Q’ Q 0 String “Q”
  • 12.
    String Comparison (1/2) •Suppose there are two strings, str1 and str2. – The condition str1 < str2 compare the initial memory address of str1 and of str2. • The comparison between two strings is done by comparing each corresponding character in them. – The characters are comapared against the ASCII table. – “thrill” < “throw” since ‘i’ < ‘o’; – “joy” < joyous“; • The standard string comparison uses the strcmp and strncmp functions.
  • 13.
    String Comparison (2/2) RelationshipReturned Value Example str1 < str2 Negative “Hello”< “Hi” str1 = str2 0 “Hi” = “Hi” str1 > str2 Positive “Hi” > “Hello” • e.g., we can check if two strings are the same by if(strcmp(str1, str2) != 0) printf(“The two strings are different!”);
  • 14.
    Array of Strings •Table of strings is called as Array of strings. Example: char name[3][10]={“Sharda”,”Neelima”,”Geeta”}; //(Array of strings)Two dimensional array of strings • Name is an array of 3 strings;each containing 10 characters.The total storage for name requires 30 bytes as below. S h a r d a 0 N e e l i m a 0 G e e t a 0