You are on page 1of 20

19CSE102 


Computer Programming
Strings
Overview

! Introduction
! Usage
! Declaration
! Initialization
! Read from User
! gets() and puts()
! Strings and functions
! Internal string functions
! String as 2D array
Introduction

! In C programming, a string is an array of characters terminated with a null


character \0. For example:
! "c string"
! When the compiler encounters a sequence of characters enclosed in the
double quotation marks, it appends a null character \0 at the end.
! Each character is stored in memory as ASCII value
ASCII Value Table
Declaration

! Before you can work with strings, you need to declare them first. Since string
is an array of characters. You declare strings in a similar way like you do with
arrays.
! Here's how you declare a string:
! char s[5];
Initialization

! You can initialize strings in a number of ways.


! char c[] = "abcd";
! char c[50] = "abcd";
! char c[] = {'a', 'b', 'c', 'd', '\0'};
! char c[5] = {'a', 'b', 'c', 'd', '\0'};
Read from user

! You can use the scanf() function to read a string.


! The scanf() function reads the sequence of characters until it
encounters whitespace(space, newline, tab, etc.).
Read from user

#include <stdio.h>

int main()
{
char name[20];
printf("Enter name: "); Enter name: Amrita
Your name is Amrita.
scanf("%s", name);
printf("Your name is %s.\n", name);
return 0;
}
gets() and puts()

! You can use gets() function to read a line of string. And, you can


use puts() to display the string.
gets() and puts()

#include <stdio.h>
Enter name: I am studying at Amrita
int main() Name: I am studying at Amrita
{
char name[30];
printf("Enter name: ");
gets(name); // read string
printf("Name: ");
puts(name); // display string
return 0;
}
Strings as function parameters

! Strings can be passed to a function in a similar way as arrays.


! Recap Passing Arrays
! One Dimensional Arrays
! float calculateSum(float num[]); //Declaration
! result = calculateSum(num); //Calling
! Passing Array Elements???
! Multidimensional Arrays
! void displayNumbers(int num[2][2]); //Declaration
! displayNumbers(num); // Calling
! Note: When passing two-dimensional arrays, it is not mandatory to specify the number of
rows in the array. However, the number of columns should always be specified.
Strings as function parameters

#include <stdio.h>
void displayString(char str[]); Enter string: Amrita School of Engineering
int main() String Output: Amrita School of Engineering
{
char str[50];
printf("Enter string: ");
gets(str);
displayString(str); // Passing string to a function.
return 0;
}

void displayString(char str[])


{
printf("String Output: ");
puts(str);
}
Internal string functions

! All the internal string functions are included in “string.h” header file.
! It has to be included in your code to use the internal functions.
! strlen(): The function takes a single argument, i.e, the string variable
whose length is to be found, and returns the length of the string passed.
! strcpy() : Function copies the string to another character array.
! strcmp(): The strcmp() function compares two strings and returns 0 if
both strings are identical.
! strcat(): concatenates (joins) two strings.
Internal string functions

! The strcmp() compares two strings character by character. If the first


character of two strings are equal, next character of two strings are
compared. This continues until the corresponding characters of two strings
are different or a null character '\0' is reached.
! Return Value from strcmp()

Return Value Remarks


0 if both strings are identical (equal)
negative if the ASCII value of first unmatched character is less than second.
positive integer if the ASCII value of first unmatched character is greater than second.
Internal string functions
//Code to demonstrate string internal functions
//Copying string2 content to string3 using for loop
#include <stdio.h>
#include <string.h> for (i=0; string2[i]!= '\0'; i++) {
string3[i] = string2[i];
int main() }
{
char string1[100], string2[100], string3[100]; string3[i] = '\0';
int len1, len2; printf("String3 after copying String2\n");
int i; puts(string3);
printf("Enter String1:");
gets(string1); //Taking input using gets
printf("Enter String2:");
// Comparing string2 and string3 using strcmp() function
gets(string2); if(! strcmp(string3, string2))
printf("string3 and string2 are equal\n");
//Printing length of string1 and string2 using strlen else
len1 = strlen(string1); printf("string3 and string2 are not equal\n");
len2 = strlen(string2);
printf("Length of string1 is %d\n", len1);
printf("Length of string2 is %d\n", len2); // Concatinating string1 and string3 and store the result
string1 using strcat() function
//Copying string1 content to string3 using inbuilt funciton strcat(string1, string3);
printf("Data in string1 after contcatination:");
strcpy(string3, string1);
printf("String3-"); puts(string1);
puts(string3); }
Internal string functions

Enter String1:Amrita School


Enter String2:of Engineering
Length of string1 is 13
Length of string2 is 14
String3-Amrita School
String3 after copying String2
of Engineering
string3 and string2 are equal
Data in string1 after contcatination:Amrita Schoolof Engineering
String as 2D array

! How a list of student names is stored ??


! We can use a 2D array of strings
! In each row one name
! Declaration and Initialization are the same as a typical 2d array.
Taking multiple students name as input and print the same

//Taking multiple students name as input and print the same


#include <stdio.h>

int main() Enter the number of students:3


{ Enter all the names:
char names [100][100];
int i, j; Amrita
int count; School
char dummy;
Engineering
printf("Enter the number of students:"); The names are:
scanf("%d", &count); Amrita
scanf("%c",&dummy); //to igonre the "enter" typed after the
previous output School
printf("Enter all the names:\n"); Engineering
//Reading values from user
for(i=0;i<count;i++) {
gets(names[i]);
}

//printing the name list


for(i=0;i<count;i++) {
puts(names[i]);
}
}
Practice Problem

! Write a C program to Sort Strings in Dictionary Order


Thank You …

You might also like