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

ppsc unit3 strings-1 by anesu chirozva

The document is a syllabus for a programming course focused on problem-solving using C, covering topics such as arrays, strings, sorting, and searching techniques. It includes detailed explanations of string handling functions and their usage in C programming. Additionally, it provides code examples for various string operations like initialization, reading, copying, concatenation, and comparison.
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

ppsc unit3 strings-1 by anesu chirozva

The document is a syllabus for a programming course focused on problem-solving using C, covering topics such as arrays, strings, sorting, and searching techniques. It includes detailed explanations of string handling functions and their usage in C programming. Additionally, it provides code examples for various string operations like initialization, reading, copying, concatenation, and comparison.
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/ 21

Aditya University

FRESHMAN ENGINEERING DEPARTMENT


Extend a Hearty Welcome
to

Programming
for
Problem Solving Using C

G. Sandhya Devi
Assistant Professor,
Freshmen Engineering Department
Syllabus

UNIT – III
Arrays indexing, Accessing programs with array of integers, two dimensional arrays,
Introduction to Strings, string handling functions.
Sorting techniques: bubble sort, selection sort.
Searching Techniques: linear , Binary search.

2
String

• In C programming, a string is a sequence of characters terminated with a


null character \0.
• A string can be declared using character array in c.
Declaration of strings:
• Declaring a string is as simple as declaring a one-dimensional array. Below
is the basic syntax for declaring a string.
syntax:
char string_name[size];
//string name is any name given to the string variable and size is used
to define the length of the string
example:
char mystring[20];
Initializing a String

A string can be initialized in different ways.


Examples:
A d i t y a \0
char str[] = “Aditya";

char str[50] = " Aditya";

char str[7] = {‘A’,’d',’i',’t',’y',’a’,'\0'};

char str[] = {‘A’,’d',’i',’t',’y',’a’,'\0'};


Initializing a String

#include<stdio.h>
printf("%s\n",str1);
int main()
printf("%s\
{
n",str2);
char str1[] = "Aditya";
printf("%s\n",str3);
printf("%s\
char str2[7] = "Aditya";
return 0;
n",str4);
}
char str3[7] = {'A','d','i','t','y','a','\0'};

char str4[] = {'A','d','i','t','y','a','\0'};


Reading a String

scanf can be used to read a string from keyboard


syntax:
scanf(“%s”,stringname); Execution:
Enter the string?Aditya
#include<stdio.h>
You entered Aditya
int main ()
{
char str[20]; Enter the string?Aditya College
printf("Enter the string?"); You entered Aditya
scanf("%s",str); So,this scanf not reading entire string.it reads
printf("You entered %s",s); upto first word
return 0;
}
Reading a String

scanf to read string with spaces

syntax: scanf("%[^\ Execution:


n]s",stringname); Enter the string?Aditya
You entered Aditya
#include<stdio.h>
int main ()
{
char str[20]; Enter the string?Aditya College
printf("Enter the string?"); You entered Aditya College
scanf("%[^\n]s",str);
printf("You entered %s",str);
return 0;
}
Reading a String using gets(),fgets()

#include<stdio.h>
gets() to read string with spaces int main ()
{
syntax: char str[20];
gets(stringname); printf("Enter the string?");
gets(str);
printf("You entered %s", str);
fgets() to read string with spaces return 0;
}
syntax: o/p for both programs:
Enter the string?Aditya College
fgets(stringname,size,stdin); #include<stdio.h> You entered Aditya College
int main ()
{
char str[20];
printf("Enter the string?");
fgets(str,20,stdin)
printf("You entered %s",str);
return 0;}
String handling functions
string functions defined in "string.h" library.
No. Function Description
1) strlen(string_name) returns the length of string name.
2) strcpy(destination, source) copies the contents of source string to destination
string.

3) strcat(first_string, concatinates or joins first string with second string.


second_string) The result of the string is stored in first string.

4) strcmp(first_string, compares the first string with second string. If both


second_string) strings are same, it returns 0.

5) strrev(string) returns reverse string.


6) strlwr(string) returns string characters in lowercase.
7) strupr(string) r eFtOuR PrRnOBsLEMstSrOiVL nNI gG UcShINGaCracters in uppercase
N G
string operations using string handling functions
#include<stdio.h>
#include<string.h>
int main () {
char str1[20],str2[20],str3[20];
printf("\nEnter the string1");
fgets(str1,20,stdin);
printf("\n enter string2");
fgets(str2 ,20,stdin);
printf("length of string1 is %lu",strlen(str1));
strcat(str1,str2);
printf("\n concatenation of string1,string2 is
%s ",str1);
strcpy(str3,str1);
printf("\n string1 is %s",str1);
printf("\n string2 is%s",str2);
printf("\nstring3 is %s",str3);
printf("\n comparison is
%d",strcmp(str2,str3));
return 0;
Strlen( )

This function can be used to find a string’s length.


#include <stdio.h>
#include <string.h>
int main()
{
char a[20]="Program";
char b[20]={'P','r','o','g','r','a','m','\0'};
printf("Length of string a = % \n",strlen(a));
printf("Length of string b = %z \n",strlen(b));

return 0;
}
Run Code
Output
Length of string a = 7
Length of string b = 7
Strcpy( )

The strcpy() function copies the string pointed by source (including the null
character) to the destination.
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = “good";
char str2[20];
// copying str1 to str2
strcpy(str2, str1);
puts(str2);
return 0;
}
Output
good
Strcmp( )
This function is used to compare two strings.
Example:
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
return 0;
}
Output
strcmp(str1, str2) = 1
strcmp(str1, str3) = 0
Strcat( )

Strcat()
This function is used to concatenate two strings.
int main()
{
char str1[] = "Hello";
char str2[] = "World";
strcat(str1, str2);
printf("The concatenated string is: %s\n", str1);
return 0;
}
Output:
The concatenated string is: Hello World
Strupr( ) & Strlwr()

Strupr() Strlwr()
It takes a string as input and converts all the
It takes a string as input and converts all the
letters in the string to lowercase.
letters in the string to uppercase.
int main()
int main() {
{
char str[] = "Hello, World!";
char str[] = "Hello, World!";
printf("Original string: %s\n", str);
printf("Original string: %s\n", str);
strupr(str);
// Convert string to lowercase
printf("Uppercase string: %s\n", str);
strlwr(str);
return 0;
printf("Lowercase string: %s\n", str);
}
return 0;
Output:
}
Original string: Hello, World!
Output:
Uppercase string: HELLO, WORLD!
Original string: Hello, World!
Lowercase string: hello, world!
Strrev( )

Strrev()
It is used to reverse a given string. It takes a string as an argument and returns a
pointer to the reversed string.
int main()
{
char str[40]; // declare the size of character string
printf(" \n Enter a string to be reversed: ");
scanf("%s", str);
printf(" \n After the reverse of a string: %s ", strrev(str));
return 0;
}
Output:
Enter a string to be reversed: apple
After the reverse of a string: elppa
length of
string operationsstring
without using string handling functions
#include<stdio.h>
int main()
{
char str [100];
int length, i ;
printf ("\nEnter the String : ");
gets(str);
length=0;
for(i=0;str[i]!='\0';i++)
{ Output:
length ++; Enter the String : aditya college
Length of the String is : 14
}
printf("\nLength of the String is : %d",length );
return 0;
}
string copy

#include<stdio.h>
int main()
{
char s1 [100] , s2 [100]; int i=0;
printf ("\nEnter the string :");
gets ( s1 );
while(s1[i]!= '\0')
{
s2[i] = s1[i]; Output:
Enter the string :aditya college
i++; } Copied String is aditya college
s2[i] = '\0';
printf ("\nCopied String is %s ", s2 );
return 0;
}
string concatenation

#include<stdio.h> for (j = 0; s2[j] != '\0'; i++, j++)


#include<string.h> {
int main() s1[i] = s2[j]; }
{ s1[i] = '\0';
char s1[50], s2[30]; printf("\nConcated string is :%s", s1);
printf("\nEnter String 1 :"); return (0);
gets(s1); }
printf("\nEnter String 2 :"); Output:
gets(s2); Enter String 1 :sandhya
Enter String 2 :devi
int i, j; Concated string is :sandhyadevi
i = strlen(s1);
string comparison
#include<stdio.h>
#include<string.h> if(flag==0)
main(){ printf("\nTwo strings are equals ");
char s1[50],s2[30]; else
int i,j,flag=0; printf("\nTwo strings are not equal ");
printf("\nEnter String1:"); }
gets(s1);
printf("\nEnter String2:");
gets(s2); Output:
for(i=0,j=0;s1[i]!='\0'&&s2[j]!='\0';i++,j++) Enter String1:welcome
{ Enter String2:welcome
Two strings are equals
if( s1 [ i ]!= s2 [ j ])
{
flag ++;
break;
}}
string reverse

#include<stdio.h> for(i=len-1;i>=0;i--)
int main() {
{ printf("%c",s1[i]);
char s1[20]; }
int i,len; return 0;
printf("Reverse the string "); }
printf("\nEnter the string: ");
scanf(" %[^\n]s",s1);
Output:
while(s1[i]!='\0') Reverse the string
{ Enter the string: aditya college'
len=len+1; 'egelloc aytida
i++ ;
}

21

You might also like