SlideShare a Scribd company logo
Aditya University
FRESHMAN ENGINEERING DEPARTMENT
G. Sandhya Devi
Assistant Professor,
Freshmen Engineering Department
Extend a Hearty Welcome
to
Programming
for
Problem Solving Using C
Syllabus
2
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.
• 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];
String
A string can be initialized in different ways.
Examples:
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'};
A d i t y a 0
Initializing a String
#include<stdio.h>
int main()
{
char str1[] = "Aditya";
char str2[7] = "Aditya";
char str3[7] = {'A','d','i','t','y','a','0'};
char str4[] = {'A','d','i','t','y','a','0'};
printf("%sn",str1);
printf("%s
n",str2);
printf("%sn",str3);
printf("%s
n",str4);
return 0;
}
Initializing a String
scanf can be used to read a string from keyboard
syntax:
scanf(“%s”,stringname);
#include<stdio.h>
int main ()
{
char str[20];
printf("Enter the string?");
scanf("%s",str);
printf("You entered %s",s);
return 0;
}
Execution:
Enter the string?Aditya
You entered Aditya
Enter the string?Aditya College
You entered Aditya
So,this scanf not reading entire string.it reads
upto first word
Reading a String
scanf to read string with spaces
syntax: scanf("%[^
n]s",stringname);
#include<stdio.h>
int main ()
{
char str[20];
printf("Enter the string?");
scanf("%[^n]s",str);
printf("You entered %s",str);
return 0;
}
Execution:
Enter the string?Aditya
You entered Aditya
Enter the string?Aditya College
You entered Aditya College
Reading a String
gets() to read string with spaces
syntax:
gets(stringname);
#include<stdio.h>
int main ()
{
char str[20];
printf("Enter the string?");
gets(str);
printf("You entered %s", str);
return 0;
}
fgets() to read string with spaces
syntax:
fgets(stringname,size,stdin); #include<stdio.h>
int main ()
{
char str[20];
printf("Enter the string?");
fgets(str,20,stdin)
printf("You entered %s",str);
return 0;}
o/p for both programs:
Enter the string?Aditya College
You entered Aditya College
Reading a String using gets(),fgets()
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,
second_string)
concatinates or joins first string with second string.
The result of the string is stored in first string.
4) strcmp(first_string,
second_string)
compares the first string with second string. If both
strings are same, it returns 0.
5) strrev(string) returns reverse string.
6) strlwr(string) returns string characters in lowercase.
7) strupr(string) NrGeFtOuR PrRnO
BsL
E
MstSrOiL
VnI
NgG UcShI
N
GaCracters in uppercase
String handling functions
string functions defined in "string.h" library.
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;
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
Strlen( )
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
Strcpy( )
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) = %dn", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %dn", result);
return 0;
}
Output
strcmp(str1, str2) = 1
strcmp(str1, str3) = 0
Strcmp( )
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: %sn", str1);
return 0;
}
Output:
The concatenated string is: Hello World
Strcat( )
Strupr()
It takes a string as input and converts all the
letters in the string to uppercase.
int main() {
char str[] = "Hello, World!";
printf("Original string: %sn", str);
strupr(str);
printf("Uppercase string: %sn", str);
return 0;
}
Output:
Original string: Hello, World!
Uppercase string: HELLO, WORLD!
Strlwr()
It takes a string as input and converts all the
letters in the string to lowercase.
int main()
{
char str[] = "Hello, World!";
printf("Original string: %sn", str);
// Convert string to lowercase
strlwr(str);
printf("Lowercase string: %sn", str);
return 0;
}
Output:
Original string: Hello, World!
Lowercase string: hello, world!
Strupr( ) & Strlwr()
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
Strrev( )
string operations without using string handling functions
length of string
#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++)
{
length ++;
}
printf("nLength of the String is : %d",length );
return 0;
}
Output:
Enter the String : aditya college
Length of the String is : 14
string copy
Output:
Enter the string :aditya college
Copied String is aditya college
#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];
i++; }
s2[i] = '0';
printf ("nCopied String is %s ", s2 );
return 0;
}
string concatenation
#include<stdio.h>
#include<string.h>
int main()
{
char s1[50], s2[30];
printf("nEnter String 1 :");
gets(s1);
printf("nEnter String 2 :");
gets(s2);
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '0'; i++, j++)
{
s1[i] = s2[j]; }
s1[i] = '0';
printf("nConcated string is :%s", s1);
return (0);
}
Output:
Enter String 1 :sandhya
Enter String 2 :devi
Concated string is :sandhyadevi
string comparison
#include<stdio.h>
#include<string.h>
main(){
char s1[50],s2[30];
int i,j,flag=0;
printf("nEnter String1:");
gets(s1);
printf("nEnter String2:");
gets(s2);
for(i=0,j=0;s1[i]!='0'&&s2[j]!='0';i++,j++)
{
if( s1 [ i ]!= s2 [ j ])
{
flag ++;
break;
}}
if(flag==0)
printf("nTwo strings are equals ");
else
printf("nTwo strings are not equal ");
}
Output:
Enter String1:welcome
Enter String2:welcome
Two strings are equals
21
string reverse
#include<stdio.h>
int main()
{
char s1[20];
int i,len;
printf("Reverse the string ");
printf("nEnter the string: ");
scanf(" %[^n]s",s1);
while(s1[i]!='0')
{
len=len+1;
i++ ;
}
for(i=len-1;i>=0;i--)
{
printf("%c",s1[i]);
}
return 0;
}
Output:
Reverse the string
Enter the string: aditya college'
'egelloc aytida

More Related Content

Similar to programming for problem solving using C-STRINGSc (20)

PPTX
introduction to strings in c programming
mikeymanjiro2090
 
PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PDF
String notes
Prasadu Peddi
 
PPT
Strings in c
vampugani
 
PPTX
String predefined functions in C programming
ProfSonaliGholveDoif
 
PPT
14 strings
Rohit Shrivastava
 
DOCX
Unitii string
Sowri Rajan
 
PPT
Strings(2007)
svit vasad
 
PPTX
Strings CPU GTU
Maharshi Dave
 
PPTX
C programming - String
Achyut Devkota
 
PPTX
Strings
Saranya saran
 
PDF
Character Array and String
Tasnima Hamid
 
PPTX
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
PDF
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 6.pdf
amanpathak160605
 
PPTX
Week6_P_String.pptx
OluwafolakeOjo
 
PPTX
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
monimhossain14
 
PPTX
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
PPTX
Presentation more c_programmingcharacter_and_string_handling_
KarthicaMarasamy
 
introduction to strings in c programming
mikeymanjiro2090
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
String notes
Prasadu Peddi
 
Strings in c
vampugani
 
String predefined functions in C programming
ProfSonaliGholveDoif
 
14 strings
Rohit Shrivastava
 
Unitii string
Sowri Rajan
 
Strings(2007)
svit vasad
 
Strings CPU GTU
Maharshi Dave
 
C programming - String
Achyut Devkota
 
Strings
Saranya saran
 
Character Array and String
Tasnima Hamid
 
POEPPSGNHwkejnkweoewnkenjwjewkjewoewkjewijewjk
hanumanthumanideeph6
 
C_Dayyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy 6.pdf
amanpathak160605
 
Week6_P_String.pptx
OluwafolakeOjo
 
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
monimhossain14
 
UNIT 4C-Strings.pptx for c language and basic knowledge
2024163103shubham
 
Presentation more c_programmingcharacter_and_string_handling_
KarthicaMarasamy
 

Recently uploaded (20)

PPTX
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
PPTX
Introduction to Design of Machine Elements
PradeepKumarS27
 
PPTX
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
PDF
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
PPTX
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
PDF
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
PPTX
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
PPTX
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
PPTX
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
PDF
Design Thinking basics for Engineers.pdf
CMR University
 
PDF
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
PDF
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
PDF
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
PPTX
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
PPTX
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
PPTX
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
PDF
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
PPTX
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
PDF
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
PPTX
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
GitOps_Without_K8s_Training_detailed git repository
DanialHabibi2
 
Introduction to Design of Machine Elements
PradeepKumarS27
 
Worm gear strength and wear calculation as per standard VB Bhandari Databook.
shahveer210504
 
AI TECHNIQUES FOR IDENTIFYING ALTERATIONS IN THE HUMAN GUT MICROBIOME IN MULT...
vidyalalltv1
 
Arduino Based Gas Leakage Detector Project
CircuitDigest
 
Pressure Measurement training for engineers and Technicians
AIESOLUTIONS
 
MobileComputingMANET2023 MobileComputingMANET2023.pptx
masterfake98765
 
美国电子版毕业证南卡罗莱纳大学上州分校水印成绩单USC学费发票定做学位证书编号怎么查
Taqyea
 
Server Side Web Development Unit 1 of Nodejs.pptx
sneha852132
 
Design Thinking basics for Engineers.pdf
CMR University
 
International Journal of Information Technology Convergence and services (IJI...
ijitcsjournal4
 
Ethics and Trustworthy AI in Healthcare – Governing Sensitive Data, Profiling...
AlqualsaDIResearchGr
 
Basic_Concepts_in_Clinical_Biochemistry_2018كيمياء_عملي.pdf
AdelLoin
 
Element 7. CHEMICAL AND BIOLOGICAL AGENT.pptx
merrandomohandas
 
What is Shot Peening | Shot Peening is a Surface Treatment Process
Vibra Finish
 
Element 11. ELECTRICITY safety and hazards
merrandomohandas
 
Viol_Alessandro_Presentazione_prelaurea.pdf
dsecqyvhbowrzxshhf
 
265587293-NFPA 101 Life safety code-PPT-1.pptx
chandermwason
 
Biomechanics of Gait: Engineering Solutions for Rehabilitation (www.kiu.ac.ug)
publication11
 
Mechanical Design of shell and tube heat exchangers as per ASME Sec VIII Divi...
shahveer210504
 
Ad

programming for problem solving using C-STRINGSc

  • 1. Aditya University FRESHMAN ENGINEERING DEPARTMENT G. Sandhya Devi Assistant Professor, Freshmen Engineering Department Extend a Hearty Welcome to Programming for Problem Solving Using C
  • 2. Syllabus 2 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.
  • 3. • 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]; String
  • 4. A string can be initialized in different ways. Examples: 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'}; A d i t y a 0 Initializing a String
  • 5. #include<stdio.h> int main() { char str1[] = "Aditya"; char str2[7] = "Aditya"; char str3[7] = {'A','d','i','t','y','a','0'}; char str4[] = {'A','d','i','t','y','a','0'}; printf("%sn",str1); printf("%s n",str2); printf("%sn",str3); printf("%s n",str4); return 0; } Initializing a String
  • 6. scanf can be used to read a string from keyboard syntax: scanf(“%s”,stringname); #include<stdio.h> int main () { char str[20]; printf("Enter the string?"); scanf("%s",str); printf("You entered %s",s); return 0; } Execution: Enter the string?Aditya You entered Aditya Enter the string?Aditya College You entered Aditya So,this scanf not reading entire string.it reads upto first word Reading a String
  • 7. scanf to read string with spaces syntax: scanf("%[^ n]s",stringname); #include<stdio.h> int main () { char str[20]; printf("Enter the string?"); scanf("%[^n]s",str); printf("You entered %s",str); return 0; } Execution: Enter the string?Aditya You entered Aditya Enter the string?Aditya College You entered Aditya College Reading a String
  • 8. gets() to read string with spaces syntax: gets(stringname); #include<stdio.h> int main () { char str[20]; printf("Enter the string?"); gets(str); printf("You entered %s", str); return 0; } fgets() to read string with spaces syntax: fgets(stringname,size,stdin); #include<stdio.h> int main () { char str[20]; printf("Enter the string?"); fgets(str,20,stdin) printf("You entered %s",str); return 0;} o/p for both programs: Enter the string?Aditya College You entered Aditya College Reading a String using gets(),fgets()
  • 9. 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, second_string) concatinates or joins first string with second string. The result of the string is stored in first string. 4) strcmp(first_string, second_string) compares the first string with second string. If both strings are same, it returns 0. 5) strrev(string) returns reverse string. 6) strlwr(string) returns string characters in lowercase. 7) strupr(string) NrGeFtOuR PrRnO BsL E MstSrOiL VnI NgG UcShI N GaCracters in uppercase String handling functions string functions defined in "string.h" library.
  • 10. 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;
  • 11. 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 Strlen( )
  • 12. 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 Strcpy( )
  • 13. 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) = %dn", result); // comparing strings str1 and str3 result = strcmp(str1, str3); printf("strcmp(str1, str3) = %dn", result); return 0; } Output strcmp(str1, str2) = 1 strcmp(str1, str3) = 0 Strcmp( )
  • 14. 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: %sn", str1); return 0; } Output: The concatenated string is: Hello World Strcat( )
  • 15. Strupr() It takes a string as input and converts all the letters in the string to uppercase. int main() { char str[] = "Hello, World!"; printf("Original string: %sn", str); strupr(str); printf("Uppercase string: %sn", str); return 0; } Output: Original string: Hello, World! Uppercase string: HELLO, WORLD! Strlwr() It takes a string as input and converts all the letters in the string to lowercase. int main() { char str[] = "Hello, World!"; printf("Original string: %sn", str); // Convert string to lowercase strlwr(str); printf("Lowercase string: %sn", str); return 0; } Output: Original string: Hello, World! Lowercase string: hello, world! Strupr( ) & Strlwr()
  • 16. 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 Strrev( )
  • 17. string operations without using string handling functions length of string #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++) { length ++; } printf("nLength of the String is : %d",length ); return 0; } Output: Enter the String : aditya college Length of the String is : 14
  • 18. string copy Output: Enter the string :aditya college Copied String is aditya college #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]; i++; } s2[i] = '0'; printf ("nCopied String is %s ", s2 ); return 0; }
  • 19. string concatenation #include<stdio.h> #include<string.h> int main() { char s1[50], s2[30]; printf("nEnter String 1 :"); gets(s1); printf("nEnter String 2 :"); gets(s2); int i, j; i = strlen(s1); for (j = 0; s2[j] != '0'; i++, j++) { s1[i] = s2[j]; } s1[i] = '0'; printf("nConcated string is :%s", s1); return (0); } Output: Enter String 1 :sandhya Enter String 2 :devi Concated string is :sandhyadevi
  • 20. string comparison #include<stdio.h> #include<string.h> main(){ char s1[50],s2[30]; int i,j,flag=0; printf("nEnter String1:"); gets(s1); printf("nEnter String2:"); gets(s2); for(i=0,j=0;s1[i]!='0'&&s2[j]!='0';i++,j++) { if( s1 [ i ]!= s2 [ j ]) { flag ++; break; }} if(flag==0) printf("nTwo strings are equals "); else printf("nTwo strings are not equal "); } Output: Enter String1:welcome Enter String2:welcome Two strings are equals
  • 21. 21 string reverse #include<stdio.h> int main() { char s1[20]; int i,len; printf("Reverse the string "); printf("nEnter the string: "); scanf(" %[^n]s",s1); while(s1[i]!='0') { len=len+1; i++ ; } for(i=len-1;i>=0;i--) { printf("%c",s1[i]); } return 0; } Output: Reverse the string Enter the string: aditya college' 'egelloc aytida