Lecture 17
In the previous lecture, we have seen some of
the string library functions. The best use of pointers to manipulate string can
be illustrated with the help of making those library functions into user
defined functions- which means you can develop user defined functions that will
perform exactly the same way strln ( ), strcpy ( ), and strcmp ( ) works.
strln ( )
Take a look at the following program. It
performs the same task the library function strln () does.
#include<stdio.h>
#include<conio.h>
int strlen(char *);
void main(){
clrscr();
char
str1[]="Dept of CSE";
int
len1,len2;
len1=strlen(str1);
len2=strlen("Department
of CSE");
printf("String:
%s, Length: %d\n",str1,len1);
printf("String:
%s, Length: %d","Department of CSE",len2);
getch();
}
int strlen(char *s){
int
length=0;
while
(*s!='\0'){
length++;
s++;
}
return
length;
}
strcpy (
)
Take a look at the following program. It
performs the same task the library function strcpy () does.
#include<stdio.h>
#include<conio.h>
void strcpy(char *,char *);
void main(){
clrscr();
char
source[]="Dept of CSE";
char
target[20];
strcpy(source,target);
printf("Source:
%s, Target: %s\n",source,target);
getch();
}
void strcpy(char *s,char *t){
while (*s!='\0'){
*t=*s;
s++;
t++;
}
*t='\0';
}
strcmp (
)
Take a look at the following program. It
performs the same task the library function strcmp () does.
#include<stdio.h>
#include<conio.h>
int strcmp(char *,char *);
void main(){
clrscr();
int variable;
char
str1[20],str2[20];
printf("Enter
String 1: ");
gets(str1);
printf("Enter
String 2: ");
gets(str2);
variable=strcmp(str1,str2);
printf("%d\n",variable);
getch();
}
int strcmp(char *s1,char *s2){
while (*s1
== *s2){
if(*s1
== '\0')
return
0;
s1++;
s2++;
}
return
*s1-*s2;
}
strcat (
)
Take a look at the following program. It
performs the same task the library function strcat () does.
#include<stdio.h>
#include<conio.h>
void strcat(char *,char *);
void main(){
clrscr();
int variable;
char
str1[20],str2[20];
printf("Enter
String 1: ");
gets(str1);
printf("Enter
String 2: ");
gets(str2);
strcat(str1,str2);
printf("%s",str1);
getch();
}
void strcat(char *s1,char *s2){
while
(*s1!='\0'){
s1++;
}
while
(*s2!='\0'){
*s1=*s2;
s1++;
s2++;
}
*s1='\0';
}
I hope that you will understand the use of
pointers in string manipulations after I finish my lecture and after you
conduct these programs by yourself.
4 comments:
This is great blog! Keep it up. chemical overhaul aircon price
This is great blog! Keep it up. Read About Orange County CBD
Hi everyone!
If you're looking for a comprehensive guide to Singapore, you won't want to miss out on Saturday Reviews. This online blog is dedicated to providing you with all the information you need to discover Singapore like a local. From its rich culture to its dynamic lifestyle, Saturday Reviews covers it all.
Whether you're planning a trip to Singapore, or just want to learn more about the city-state, Saturday Reviews has got you covered. The blog is updated regularly with articles that showcase the best of what Singapore has to offer, from its famous hawker centers to its iconic landmarks.
So, if you want to know more about this fascinating city, be sure to check out Saturday Reviews. Trust me, you won't regret it!
Best regards,
A fan of .Saturday Reviews
If you want to read about full guide of flat roofing then click on this and it will give you full informationFlat Roofing
Post a Comment