SlideShare a Scribd company logo
UNIT 4 C
 String: Introduction,
 predefined string functions,
 Manipulation of text data,
 Command Line Arguments.
• Strings are defined as an array of characters.
• The difference between a character array and a string is the string is
terminated with a special character ‘0’.
Declaration of strings:
• Declaring a string is as simple as declaring a one dimensional array.
• char str_name[size];
•
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
String predefined functions
Strlen
Strcat
Strcpy
UNIT 4C-Strings.pptx for c language and basic knowledge
a) strlen
• calculates the length of string
• strlen doesn't count '0' while calculating the length of a string.
b) Strcpy
Copies a string into another.
strcpy(s1, s2) copies the second string s2 to the first string s1.
C) Strcmp –
strcmp(s1, s2) compares two strings and finds out whether they are same or different.
It compares the two strings character by character till there is a mismatch.
If the two strings are identical, it returns a 0. If not, then it returns the difference between
the ASCII values of the first non-matching pair of characters.
d)
UNIT 4C-Strings.pptx for c language and basic knowledge
e)strlwr()
• The strlwr method is used to convert all the characters in a given string into
lowercase.
f) strupr()
• The strupr(string) function returns string characters in uppercase.
g) strstr()
The strstr() function returns pointer to the first occurrence of the matched string in the given string.
It is used to return substring from first match till the last character.
Syntax: char *strstr(const char *string, const char *match)
String programs without
using built-in function
/* C program to find the length of a string without using the built-in function */
void main()
{
char string[50];
int i, length = 0;
printf("Enter a string n");
gets(string);
for (i = 0; string[i] != '0'; i++) /* keep going through each character of the string till its end */
{
length++;
}
printf("The length of a string is the number of characters in it n");
printf("So, the length of %s = %dn", string, length);
}
Program : Reverse String Without Using Library Function [ Strrev ]
#include<stdio.h>
#include<string.h>
int main() {
char str[100], temp;
int i, j = 0;
printf("nEnter the string :");
gets(str);
i = 0;
j = strlen(str) - 1;
while (i < j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++;
j--;
}
printf("nReverse string is :%s", str);
return (0); }
UNIT 4C-Strings.pptx for c language and basic knowledge
UNIT 4C-Strings.pptx for c language and basic knowledge
Program : Copy One String into Other Without Using Library Function.
#include<stdio.h>
int main() {
char s1[100], s2[100];
int i;
printf("nEnter the string :");
gets(s1);
i = 0;
while (s1[i] != '0’)
{ s2[i] = s1[i];
i++;
}
s2[i] = '0';
printf("nCopied String is %s ", s2);
return (0);
}
//lower to upper case without using string function
int main()
{ char str[10];
int i=0;
printf("enter string");
scanf("%s",str);
while(str[i]!='0')
{
str[i]=str[i]-32; // change in case of upper to lower
i++;
}
printf("upper string is %s",str);
getch();
return 0;
}
//program to concatenate two strings
#include<stdio.h>
#include<string.h>
void concat(char[], char[]);
int main()
{
char s1[50], s2[30];
printf("nEnter String 1 :");
gets(s1);
printf("nEnter String 2 :");
gets(s2);
concat(s1, s2);
printf("nConcated string is :%s", s1);
return (0);
}
void concat(char s1[], char s2[])
{
int i, j;
i = strlen(s1);
for (j = 0; s2[j] != '0'; i++, j++) {
s1[i] = s2[j];
}
s1[i] = '0';
}
UNIT 4C-Strings.pptx for c language and basic knowledge
//program to find string is palindrome or not using string function
What is Palindrome Number ?
• a word, phrase, or sequence that
reads the same backwards as
forwards
• Some palindrome strings
examples are "dad", "radar",
"madam" etc.
How check given string is
palindrome number or not ?
•
This program works as follows :-
at first we copy the entered string
into a new string, and then we
reverse the new string and then
compares it with original string. If
both of them have same
sequence of characters i.e. they
are identical then the entered
string is a palindrome otherwise
not.
#include <stdio.h>
#include <conio.h>
void main() {
char *a;
int i,len,flag=0;
clrscr();
printf("nENTER A STRING: ");
gets(a);
len=strlen(a);
for (i=0;i<len;i++) {
if(a[i]==a[len-i-1])
flag=flag+1;
}
if(flag==len)
printf("nTHE STRING IS PALINDROM"); else
printf("nTHE STRING IS NOT PALINDROM");
getch();
}
//program to find string is palindrome or not without using string function
What is Palindrome Number ?
• a word, phrase, or sequence that
reads the same backwards as
forwards
• Some palindrome strings
examples are "dad", "radar",
"madam" etc.
How check given string is
palindrome number or not ?
•
This program works as follows :-
at first we copy the entered string
into a new string, and then we
reverse the new string and then
compares it with original string. If
both of them have same
sequence of characters i.e. they
are identical then the entered
string is a palindrome otherwise
not.
#include <stdio.h>
#include <string.h>
int main() {
char text[100];
int begin, middle, end, length = 0;
gets(text);
while ( text[length] != '0' )
length++;
end = length - 1;
middle = length/2;
for ( begin = 0 ; begin < middle ; begin++ ) {
if ( text[begin] != text[end] ) {
printf("Not a palindrome.n");
break;
}
end--;
}
if( begin == middle )
printf("Palindrome.n");
return 0;
}
Palindrome program for number
#include<stdio.h>
int main()
{
int num,r,sum=0,temp;
printf("Enter a number: ");
scanf("%d",&num);
temp=num;
while(num)
{
r=num%10;
num=num/10;
sum=sum*10+r;
}
if(temp==sum)
printf("%d is a palindrome",temp);
else
printf("%d is not a palindrome",temp);
return 0;
}
//To Delete all occurrences of Character from the String
Method Used
1.we have accepted one
string from the user and
character to be deleted
from the user.
2. Now we have passed
these two parameters to
function which will delete
all occurrences of given
character from the string.
3. Whole String will be
displayed as output
except given Character.
#include<string.h>
void del(char str[], char ch);
void main()
{ char str[10];
char ch;
printf("nEnter the string : ");
gets(str);
printf("nEnter character which you want to delete : ");
scanf("%ch", &ch);
del(str, ch);
getch();
}
void del(char str[], char ch)
{ int i, j = 0;
int size;
char ch1;
char str1[10];
size = strlen(str);
for (i = 0; i < size; i++)
{ if (str[i] != ch)
{ ch1 = str[i];
str1[j] = ch1;
j++;
}
}
str1[j] = '0';
printf("ncorrected string is : %s", str1);
}
Reverse a string using pointers
#include <stdio.h>
#include <conio.h>
void main()
{
char *s;
int len,i;
clrscr();
printf("nENTER A STRING: ");
gets(s);
len=strlen(s);
printf("nTHE REVERSE OF THE STRING IS:");
for(i=len;i>=0;i--)
printf("%c",*(s+i));
getch();
}
Command line arguments
• The arguments passed from command line are called command line arguments. These arguments
are handled by main() function.
• To support command line argument, you need to change the structure of main() function as given
below.
int main(int argc, char *argv[] )
Here, argc is an integer value that specifies number of command line arguments passed into the
program, including the name of the program.
The argv[] contains the total number of arguments. The first argument is the file name always.
The array of character pointers, argv contains the list of all the arguments. argv[0] is the name of
the program or an empty string if the name is not available.
Argv[1] to argv[argc-1] specifies the command line argument.
#include <stdio.h>
void main(int argc, char *argv[] )
{
printf("Program name is: %sn", argv[0]);
if(argc < 2)
{
printf("No argument passed through command line.n");
}
else
{
printf("First argument is: %sn", argv[1]);
}
}
Ad

More Related Content

Similar to UNIT 4C-Strings.pptx for c language and basic knowledge (20)

Strings part2
Strings part2Strings part2
Strings part2
yndaravind
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
mounikanarra3
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
TAPANDDRAW
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
arunatluri
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
Samsil Arefin
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
Strings
StringsStrings
Strings
Saranya saran
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
Sasideepa
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
Sasideepa
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
JStalinAsstProfessor
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
string function with example...................
string function with example...................string function with example...................
string function with example...................
NishantsrivastavaV
 
14 strings
14 strings14 strings
14 strings
Rohit Shrivastava
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_
KarthicaMarasamy
 
unit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentationunit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
Programming in C - Fundamental Study of Strings
Programming in C - Fundamental Study of  StringsProgramming in C - Fundamental Study of  Strings
Programming in C - Fundamental Study of Strings
Dr. Chandrakant Divate
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
mikeymanjiro2090
 
Strings in programming tutorial.
Strings  in programming tutorial.Strings  in programming tutorial.
Strings in programming tutorial.
Samsil Arefin
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
Appili Vamsi Krishna
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
Sasideepa
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
Sasideepa
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
JStalinAsstProfessor
 
Lesson in Strings for C Programming Lessons
Lesson in Strings for C Programming LessonsLesson in Strings for C Programming Lessons
Lesson in Strings for C Programming Lessons
JamesChristianGadian
 
string function with example...................
string function with example...................string function with example...................
string function with example...................
NishantsrivastavaV
 
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3
 
Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_Presentation more c_programmingcharacter_and_string_handling_
Presentation more c_programmingcharacter_and_string_handling_
KarthicaMarasamy
 
unit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentationunit-5 String Math Date Time AI presentation
unit-5 String Math Date Time AI presentation
MukeshTheLioner
 
Programming in C - Fundamental Study of Strings
Programming in C - Fundamental Study of  StringsProgramming in C - Fundamental Study of  Strings
Programming in C - Fundamental Study of Strings
Dr. Chandrakant Divate
 

Recently uploaded (20)

Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
How to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of saleHow to manage Multiple Warehouses for multiple floors in odoo point of sale
How to manage Multiple Warehouses for multiple floors in odoo point of sale
Celine George
 
New Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptxNew Microsoft PowerPoint Presentation.pptx
New Microsoft PowerPoint Presentation.pptx
milanasargsyan5
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
Understanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s GuideUnderstanding P–N Junction Semiconductors: A Beginner’s Guide
Understanding P–N Junction Semiconductors: A Beginner’s Guide
GS Virdi
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Unit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdfUnit 6_Introduction_Phishing_Password Cracking.pdf
Unit 6_Introduction_Phishing_Password Cracking.pdf
KanchanPatil34
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
LDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini UpdatesLDMMIA Reiki Master Spring 2025 Mini Updates
LDMMIA Reiki Master Spring 2025 Mini Updates
LDM Mia eStudios
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public SchoolsK12 Tableau Tuesday  - Algebra Equity and Access in Atlanta Public Schools
K12 Tableau Tuesday - Algebra Equity and Access in Atlanta Public Schools
dogden2
 
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...Multi-currency in odoo accounting and Update exchange rates automatically in ...
Multi-currency in odoo accounting and Update exchange rates automatically in ...
Celine George
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Ad

UNIT 4C-Strings.pptx for c language and basic knowledge

  • 1. UNIT 4 C  String: Introduction,  predefined string functions,  Manipulation of text data,  Command Line Arguments.
  • 2. • Strings are defined as an array of characters. • The difference between a character array and a string is the string is terminated with a special character ‘0’. Declaration of strings: • Declaring a string is as simple as declaring a one dimensional array. • char str_name[size]; •
  • 10. a) strlen • calculates the length of string • strlen doesn't count '0' while calculating the length of a string.
  • 11. b) Strcpy Copies a string into another. strcpy(s1, s2) copies the second string s2 to the first string s1.
  • 12. C) Strcmp – strcmp(s1, s2) compares two strings and finds out whether they are same or different. It compares the two strings character by character till there is a mismatch. If the two strings are identical, it returns a 0. If not, then it returns the difference between the ASCII values of the first non-matching pair of characters.
  • 13. d)
  • 15. e)strlwr() • The strlwr method is used to convert all the characters in a given string into lowercase.
  • 16. f) strupr() • The strupr(string) function returns string characters in uppercase.
  • 17. g) strstr() The strstr() function returns pointer to the first occurrence of the matched string in the given string. It is used to return substring from first match till the last character. Syntax: char *strstr(const char *string, const char *match)
  • 18. String programs without using built-in function
  • 19. /* C program to find the length of a string without using the built-in function */ void main() { char string[50]; int i, length = 0; printf("Enter a string n"); gets(string); for (i = 0; string[i] != '0'; i++) /* keep going through each character of the string till its end */ { length++; } printf("The length of a string is the number of characters in it n"); printf("So, the length of %s = %dn", string, length); }
  • 20. Program : Reverse String Without Using Library Function [ Strrev ] #include<stdio.h> #include<string.h> int main() { char str[100], temp; int i, j = 0; printf("nEnter the string :"); gets(str); i = 0; j = strlen(str) - 1; while (i < j) { temp = str[i]; str[i] = str[j]; str[j] = temp; i++; j--; } printf("nReverse string is :%s", str); return (0); }
  • 23. Program : Copy One String into Other Without Using Library Function. #include<stdio.h> int main() { char s1[100], s2[100]; int i; printf("nEnter the string :"); gets(s1); i = 0; while (s1[i] != '0’) { s2[i] = s1[i]; i++; } s2[i] = '0'; printf("nCopied String is %s ", s2); return (0); }
  • 24. //lower to upper case without using string function int main() { char str[10]; int i=0; printf("enter string"); scanf("%s",str); while(str[i]!='0') { str[i]=str[i]-32; // change in case of upper to lower i++; } printf("upper string is %s",str); getch(); return 0; }
  • 25. //program to concatenate two strings #include<stdio.h> #include<string.h> void concat(char[], char[]); int main() { char s1[50], s2[30]; printf("nEnter String 1 :"); gets(s1); printf("nEnter String 2 :"); gets(s2); concat(s1, s2); printf("nConcated string is :%s", s1); return (0); } void concat(char s1[], char s2[]) { int i, j; i = strlen(s1); for (j = 0; s2[j] != '0'; i++, j++) { s1[i] = s2[j]; } s1[i] = '0'; }
  • 27. //program to find string is palindrome or not using string function What is Palindrome Number ? • a word, phrase, or sequence that reads the same backwards as forwards • Some palindrome strings examples are "dad", "radar", "madam" etc. How check given string is palindrome number or not ? • This program works as follows :- at first we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not. #include <stdio.h> #include <conio.h> void main() { char *a; int i,len,flag=0; clrscr(); printf("nENTER A STRING: "); gets(a); len=strlen(a); for (i=0;i<len;i++) { if(a[i]==a[len-i-1]) flag=flag+1; } if(flag==len) printf("nTHE STRING IS PALINDROM"); else printf("nTHE STRING IS NOT PALINDROM"); getch(); }
  • 28. //program to find string is palindrome or not without using string function What is Palindrome Number ? • a word, phrase, or sequence that reads the same backwards as forwards • Some palindrome strings examples are "dad", "radar", "madam" etc. How check given string is palindrome number or not ? • This program works as follows :- at first we copy the entered string into a new string, and then we reverse the new string and then compares it with original string. If both of them have same sequence of characters i.e. they are identical then the entered string is a palindrome otherwise not. #include <stdio.h> #include <string.h> int main() { char text[100]; int begin, middle, end, length = 0; gets(text); while ( text[length] != '0' ) length++; end = length - 1; middle = length/2; for ( begin = 0 ; begin < middle ; begin++ ) { if ( text[begin] != text[end] ) { printf("Not a palindrome.n"); break; } end--; } if( begin == middle ) printf("Palindrome.n"); return 0; }
  • 29. Palindrome program for number #include<stdio.h> int main() { int num,r,sum=0,temp; printf("Enter a number: "); scanf("%d",&num); temp=num; while(num) { r=num%10; num=num/10; sum=sum*10+r; } if(temp==sum) printf("%d is a palindrome",temp); else printf("%d is not a palindrome",temp); return 0; }
  • 30. //To Delete all occurrences of Character from the String Method Used 1.we have accepted one string from the user and character to be deleted from the user. 2. Now we have passed these two parameters to function which will delete all occurrences of given character from the string. 3. Whole String will be displayed as output except given Character. #include<string.h> void del(char str[], char ch); void main() { char str[10]; char ch; printf("nEnter the string : "); gets(str); printf("nEnter character which you want to delete : "); scanf("%ch", &ch); del(str, ch); getch(); } void del(char str[], char ch) { int i, j = 0; int size; char ch1; char str1[10]; size = strlen(str); for (i = 0; i < size; i++) { if (str[i] != ch) { ch1 = str[i]; str1[j] = ch1; j++; } } str1[j] = '0'; printf("ncorrected string is : %s", str1); }
  • 31. Reverse a string using pointers #include <stdio.h> #include <conio.h> void main() { char *s; int len,i; clrscr(); printf("nENTER A STRING: "); gets(s); len=strlen(s); printf("nTHE REVERSE OF THE STRING IS:"); for(i=len;i>=0;i--) printf("%c",*(s+i)); getch(); }
  • 33. • The arguments passed from command line are called command line arguments. These arguments are handled by main() function. • To support command line argument, you need to change the structure of main() function as given below. int main(int argc, char *argv[] ) Here, argc is an integer value that specifies number of command line arguments passed into the program, including the name of the program. The argv[] contains the total number of arguments. The first argument is the file name always. The array of character pointers, argv contains the list of all the arguments. argv[0] is the name of the program or an empty string if the name is not available. Argv[1] to argv[argc-1] specifies the command line argument.
  • 34. #include <stdio.h> void main(int argc, char *argv[] ) { printf("Program name is: %sn", argv[0]); if(argc < 2) { printf("No argument passed through command line.n"); } else { printf("First argument is: %sn", argv[1]); } }