0% found this document useful (0 votes)
7 views

ESC-M5

Nil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

ESC-M5

Nil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 22

Module 5

20-10-2023 Prof. Shilpa B V 1


• Strings: • Pointers:
• Introduction • Introduction to pointers
• string taxonomy • declaring and accessing
• operations on strings- pointer variables
• strlen()
• strcmp()
• strcat()
• strcpy()

20-10-2023 Prof. Shilpa B V 2


Strings
• Array of characters is called a string.
• It is always terminated by the NULL character – ‘\0’.
• It is a one dimensional array of characters.
• Syntax:
char string_variable[size];
• string_variable is any name given to the string variable
• size is used to define the length of the string, i.e the number of characters
strings will store.
• Example: char string[20]; /* string is not a reserved word */
char name[10] = “Yashas”
20-10-2023 Prof. Shilpa B V 3
String Taxonomy

20-10-2023 Prof. Shilpa B V 4


Assigning a string
• We can initialize a C string in 4 different ways:
1. Assigning a String Literal without Size
• Here, the name of the string str acts as a pointer because it is an array.
• char str[] = "GeeksforGeeks“;

2. Assigning a String Literal with a Predefined Size


• String literals can be assigned with a predefined size, But always account for one extra space which
will be assigned to the null character.
• char str[50] = "GeeksforGeeks";

3. Assigning Character by Character with Size


• assign a string character by character, But remember to set the end character as ‘\0’ which is a null
character.
• char str[14] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};

4. Assigning Character by Character without size


• 20-10-2023
char str[] = { 'G','e','e','k','s','f','o','r','G','e','e','k','s','\0'};
Prof. Shilpa B V 5
Read a string
#include<stdio.h>

int main()
{
char str[50]; // declaring string
scanf("%s",str); // reading string, but only one word
gets(str); // reading string, many words with space - sentence
printf("%s",str); // print string, but only one word
puts(str); // print string, many words with space - sentence
return 0;
}
20-10-2023 Prof. Shilpa B V 6
Operations on strings
• String library function
• There are several string library functions used to manipulate string and the
prototypes for these functions are in header file “string.h”.
• strlen()
• strcmp()
• strcat()
• strcpy()

20-10-2023 Prof. Shilpa B V 7


strlen()
• This function return the length of
the string. i.e. the number of #include<stdio.h>
characters in the string excluding #include<string.h>
the terminating NULL character. void main()
{
• It accepts a single argument
char str[50];
which is pointer to the first print(”Enter a string:”);
character of the string. gets(str);
• Example: char name[20]; printf(“Length of string = %d\n”,strlen(str));
gets(name); }
n = strlen(name);

20-10-2023 Prof. Shilpa B V 8


#include<stdio.h>

strcmp()
#include<string.h>
void main()
{
char str1[10],str2[10];
• This function is used to compare printf(“Enter two strings:”);
two strings. gets(str1);
gets(str2);
• If the two string match, strcmp() if(strcmp(str1,str2)==0)
return a value 0 otherwise it return {
a non-zero value. printf(“String are same\n”);
}
• It compare the strings character by else if(strcmp(str1,str2) > 0)
character and the comparison stops {
printf(“String1 is larger than string2”);
when }
• the end of the string is reached or else
• the corresponding characters in the {
two string are not same. printf(“String1 is smaller than string2”);
}
}
20-10-2023 Prof. Shilpa B V 9
strcpy()
• This function is used to copying #include<stdio.h>
one string to another string. #include<string.h>
void main()
• The function strcpy(str2,str1) {
copies str1 to str2 including the char str1[10],str2[10];
NULL character. printf(“Enter a string:”);
scanf(“%s”,str1);
• Here str1 is the source string strcpy(str2,str1);
and str2 is the destination printf(“First string:%s, \t Second string:%s”, str1, str2);
string. strcpy(str1,”National”);
strcpy(str2,”Institute”);
• The old content if exists in string printf(“First string :%s, \tSecond string:%s”, str1, str2);
str2 are lost. }
• The function returns a pointer to
destination string str2.
20-10-2023 Prof. Shilpa B V 10
Strcat()
• This function is used to #include<stdio.h>
concatenate/append a copy of a #include<string.h>
string at the end of the other void main()
string. {
char str1[20],str[20];
• If the first string is “Computer” printf(“Enter two strings:”);
and second string is “Science” gets(str1);
then after using this function the gets(str2);
first string becomes printf(“First string:%s\t second string:%s\n”,str1,str2);
“ComputerScience”. strcat(str1,str2);
printf(“First string:%s\t second string:%s\n”,str1,str2);
• The NULL character from str1 is
strcat(str1,”&Engineering”);
removed and str2 is added at printf(“Now first string is %s\n”,str1);
the end of str1. }
• The second string str2 remains
unaffected.
20-10-2023 Prof. Shilpa B V 11
Write a C program to determine whether a given word is a
palindrome or not. Use built-in functions for string handling.
#include <stdio.h>
#include <string.h> // Keep comparing characters while they are same
while ( i < n)
void main() {
{ if (str[i++] != str[n--])
char str[20]; {
int i, n; printf("%s is not a palindrome\n", str);
return ;
printf(“Enter a string\n”); }
gets(str); }
i = 0; printf("%s is a palindrome\n", str);
n = strlen(str); return ;
n = n-1; }

20-10-2023 Prof. Shilpa B V 12


Write a C program to determine whether a given word is a
palindrome or not. Use built-in functions for string handling.
#include <stdio.h>
#include <string.h> for (i=0; i<n ; i++)
revstr[i] = str[j--]; // reverse string
void main()
{ if( strcmp( str, revstr) == 0)
char str[20],revstr[20]; printf("%s is a palindrome\n", str);
int i, n, j; else
printf("%s is not a palindrome\n", str);
printf(“Enter a string\n”);
gets(str); return;
i = 0; }
n = strlen(str);
j=n-1;
20-10-2023 Prof. Shilpa B V 13
• Program to input names of N students. Sort the names in ascending order.
Display the sorted names.

20-10-2023 Prof. Shilpa B V 14


for(i=0;i<n; i++)
for(j=i+1;j<n;j++)
# include <stdio.h> {
#include <string.h> if(strcmp(str[i],str[j]) > 0)
void main() {
strcpy(temp,str[i]);
{ strcpy(str[i],str[j]);
char names[20][25], temp[25];
int i, j, n; strcpy(str[j],temp);
}
}
printf(“Enter value of N\n”);
scanf(“%d”,&n); printf(“The sorted names with
length are:\n”);
printf(“Enter %d names\n”, n); for(i =0; i<n; i++)
for(i =0; i<n; i++) printf(“%s %d\n”, names[i],
strlen(names[i]));
scanf(“%s “, names[i]);
return;
20-10-2023 Prof. Shilpa B V 15
}
• Program to input First name and last name of N students. Display the List of
first and last names combined.

20-10-2023 Prof. Shilpa B V 16


# include <stdio.h>
#include <string.h> for(i=0;i<n;i++)
void main() {
{ strcpy(names[i], first[i]);
strcat(names[i], “ “);
char names[50][50], first[25][25], last[25][25]; strcat(names[i], last[i]);
int i, j, n; }
printf(“Enter value of N\n”); printf(“The combined names are:\n”);
scanf(“%d”,&n); for(i =0; i<n; i++)
for(i =0; i<n; i++) {
{ printf(“ \nName %d = ”, i+1);
printf(“Enter first name %d :”, i+1); puts(names[i]);
scanf(“%s “, first[i]); }
}
printf(“Enter last name %d :”, i+1);
scanf(“%s “, last[i]);
}
20-10-2023 Prof. Shilpa B V 17
Introduction to pointers
• A pointer is defined as a derived data type that can store the address of other variables or a
memory location.
• The data stored in that memory location can be accessed and manipulated using pointers
• A pointer can be used to store the memory address of other variable, function, or even other
pointer

• Syntax: datatype * ptr_variable;


where
datatype is the type of data it is pointing to.
ptr_variable is the name of the pointer.

• Example: int *ptr;  ptr points to integer data


float *p2;  p2 pointer to float type
char *p3;  p3 pointer to character type
20-10-2023 Prof. Shilpa B V 18
void main()
{
int i=105; Address Value
int *p; // pointer declaration
p=&i; // assignment i 883056844 105

printf(“value of i=%d”,*p); // value of i thru pointer p


p 883056666 883056844
printf(“value of i=%d”, i); // direct
printf(“address of i=%d”,&i); // direct
printf(“address of i=%d”,p); // address of i through p
printf(“address of p=%d”,&p); // address of p
}

20-10-2023 Prof. Shilpa B V 19


Applications of Pointers
• Pointers are used to pass information back and forth between functions.
• Pointers enable the programmers to return multiple data items from a
function via function arguments.
• Pointers provide an alternate way to access the individual elements of an
array.
• Pointers are used to pass arrays and strings as function arguments.
• Pointers are used to create complex data structures, such as trees, linked
lists, linked stacks, linked queues, and graphs
• Pointers are used for the dynamic memory allocation of a variable

20-10-2023 Prof. Shilpa B V 20


Program to swap two numbers using pointers
# include <stdio.h>
void main()
t = *x;
{
*x = *y;
int a,b,t;
*y = t;
int *x, *y;
printf(“values of a and b after swapping\n”);
printf(“Enter value of a and b\n”);
printf(“a = %d, b = %d \n”,a,b);
scanf(“%d%d”,&a,&b);
return;
printf(“values of a and b before swapping\n”);
}
printf(“a = %d, b = %d \n”,a,b);
x = &a;
y = &b;

20-10-2023 Prof. Shilpa B V 21


Program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of N real numbers.
#include<stdio.h>
#include<math.h>
mean = sum1 / n;
void main( )
ptr = a; // No & needed
{
for (i = 0; i < n; i++)
float a[20], sum1 = 0, sum2 = 0, mean, var, dev;
int i, n; {
float *ptr; sum2 = sum2 + pow ((*ptr - mean), 2);
printf ("Enter no of elements:"); ptr++;
scanf ("%d", &n); }
printf ("Enter array elements:"); var = sum2 / n;
for (i = 0; i < n; i++) dev = sqrt (var);
scanf ("%f", &a[i]); printf ("Sum :%f\n", sum1);
ptr = a; //since a is an array and it already printf ("Mean :%f\n", mean);
holds address ‘&’ need not be specified printf ("Variance :%f\n", var);
for (i = 0; i < n; i++) printf ("Deviation :%f\n", dev);
{ return;
sum = sum + *ptr; }
ptr++;
20-10-2023 Prof. Shilpa B V 22
}

You might also like