The way a group of integers can be stored in an integer array, a group of characters can also be stored in character array. An array of characters in often called a string. Character arrays are used by programming languages to manipulate text such as words and sentences.
A string constant is a one-dimensional array of characters terminated by a null ('\0') character. The terminating null ('\0') is important, because it is the only way the function that works with string can know where a strings ends. In fact, a string not terminated by a '\0' is not really a string, but merely a collection of characters.
Representation of strings
A string can be represented using an array or a linked list. A String of characters having length n can be implemented by a one dimensional array of n elements where the i th element of the array stores the i th character of the string. The advantage of such a representation is that the array elements can be directly accessed and this could speed up several operations on strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int xstrlen(char*);
void xstrcpy(char*,char*);
void xstrcat(char*,char*);
int xstrcmp(char*,char*);
void show (char*);
void main()
{
char s1[ ]="Kicit";
char s2[ ]="Nagpur";
char s3[20];
int len;
clrscr();
printf("String S1:%s\n",s1);
len=xstrlen(s1);
printf("Length of string s1:%d\n",len);
printf("String s2: %s \n",s2);
xstrcpy(s3,s1);
printf("String s3 after concatenation : %s \n",s3);
if(xstrcmp (s1,s2)==0)
printf("The strings s1 and s2 are similar \n");
else
printf("The string s1 an s2 are not similar \n");
getch();
}
/* copies source string s to target string t*/
void xstrcpy (char *t, char *s)
{
while (*s)
{
*t=*s;
t++;
s++;
}
*t='\0';
}
/*find the length of the string */
int xstrlen(char *s)
{
int l=0;
while (*s)
{
l++;
s++;
}
return l;
}
/* concatenate the two strings*/
void xstrcat (char *t, char *s)
{
while(*t)
t++;
while(*s)
*t++=*s++;
*t='\0';
}
/* compares two strings s and to for equality*/
int xstrcmp (char *t, char *s)
{
while (*s==*t)
{
if(!(*s))
return 0;
s++;
t++;
}
return(*s-*t);
}
A string constant is a one-dimensional array of characters terminated by a null ('\0') character. The terminating null ('\0') is important, because it is the only way the function that works with string can know where a strings ends. In fact, a string not terminated by a '\0' is not really a string, but merely a collection of characters.
Representation of strings
A string can be represented using an array or a linked list. A String of characters having length n can be implemented by a one dimensional array of n elements where the i th element of the array stores the i th character of the string. The advantage of such a representation is that the array elements can be directly accessed and this could speed up several operations on strings.
#include<stdio.h>
#include<conio.h>
#include<string.h>
int xstrlen(char*);
void xstrcpy(char*,char*);
void xstrcat(char*,char*);
int xstrcmp(char*,char*);
void show (char*);
void main()
{
char s1[ ]="Kicit";
char s2[ ]="Nagpur";
char s3[20];
int len;
clrscr();
printf("String S1:%s\n",s1);
len=xstrlen(s1);
printf("Length of string s1:%d\n",len);
printf("String s2: %s \n",s2);
xstrcpy(s3,s1);
printf("String s3 after concatenation : %s \n",s3);
if(xstrcmp (s1,s2)==0)
printf("The strings s1 and s2 are similar \n");
else
printf("The string s1 an s2 are not similar \n");
getch();
}
/* copies source string s to target string t*/
void xstrcpy (char *t, char *s)
{
while (*s)
{
*t=*s;
t++;
s++;
}
*t='\0';
}
/*find the length of the string */
int xstrlen(char *s)
{
int l=0;
while (*s)
{
l++;
s++;
}
return l;
}
/* concatenate the two strings*/
void xstrcat (char *t, char *s)
{
while(*t)
t++;
while(*s)
*t++=*s++;
*t='\0';
}
/* compares two strings s and to for equality*/
int xstrcmp (char *t, char *s)
{
while (*s==*t)
{
if(!(*s))
return 0;
s++;
t++;
}
return(*s-*t);
}
No comments:
Post a Comment