C++/Data Type/Char Array Function
Содержание
- 1 how to use the strcat function
- 2 memchr( ) function: Finding a character in a buffer.
- 3 memicmp( ) function: compare two string buffers.
- 4 memset( ) function: set the contents of a string buffer.
- 5 strchr( ) function: locate the first occurrence of a character in a string.
- 6 strcmp( ) function: compare two strings.
- 7 strcspn( ) function: find the occurrence of one of a group of characters.
- 8 strlen( ) function: determine the length of a string.
- 9 strstr( ) function: locate a string within a string.
- 10 strupr( ) function: convert lowercase letters to uppercase.
- 11 uses strncmp to compare two strings with the aid of the strlen function
- 12 Using memchr
- 13 Using memcmp
- 14 Using memcpy
- 15 Using memmove
- 16 Using memset
- 17 Using strerror
- 18 Using strpbrk
- 19 Using strspn:
- 20 using the strcpy function
how to use the strcat function
#include <stdio.h>
#include <string.h>
#define iSTRING_SIZE 35
main( )
{
char greeting[] = "Good morning",
name[] =" Joe, ",
message[iSTRING_SIZE];
strcpy(message,greeting);
strcat(message,name);
strcat(message,"how are you?");
printf("%s\n",message);
return(0);
}
memchr( ) function: Finding a character in a buffer.
#include <string.h>
#include <stdio.h>
char buf[35];
char *ptr;
int main( )
{
strcpy(buf,"This is a fine day for a search." );
ptr=(char *)memchr(buf,"f",35);
if (ptr != NULL)
printf("character found at location: %d\n",
ptr-buf+1);
else
printf("character not found.\n");
return (0);
}
memicmp( ) function: compare two string buffers.
#include <stdio.h>
#include <string.h>
char buf1[40],buf2[40];
int main( )
{
strcpy(buf1,"this is a test");
strcpy(buf2,"this is a test");
/* 0 - identical strings except for case */
/* x - any integer, means not identical */
printf("%d\n",memicmp(buf1,buf2,40));
/* returns a nonzero value */
return (0);
}
memset( ) function: set the contents of a string buffer.
#include <stdio.h>
#include <string.h>
char buf[20];
int main( )
{
printf("The contents of buf: %s",memset(buf,"+",15));
buf[15] = "\0";
return (0);
}
strchr( ) function: locate the first occurrence of a character in a string.
#include <stdio.h>
#include <string.h>
char s1[20] = "What is a friend?";
char *answer;
int main( )
{
answer=strchr(s1," ");
printf("After the first blank: %s\n",answer);
return (0);
}
strcmp( ) function: compare two strings.
#include <stdio.h>
#include <string.h>
char s1[45] = "test";
char s2[45] = "another test";
int answer;
int main( )
{
answer = strcmp(s1,s2);
if (answer>0)
printf("s1 is greater than s2");
else if (answer==0)
printf("s1 is equal to s2");
else
printf("s1 is less than s2");
return (0);
}
strcspn( ) function: find the occurrence of one of a group of characters.
#include <stdio.h>
#include <string.h>
char s1[35];
int answer;
int main( )
{
strcpy(s1,"We are looking for great strings." );
answer=strcspn(s1,"abc");
printf("The first a,b,c appeared at position %d\n",answer+1);
return (0);
}
strlen( ) function: determine the length of a string.
#include <stdio.h>
#include <string.h>
char *s1="String length is measured in characters!";
int main( )
{
printf("The string length is %d",strlen(s1));
return (0);
}
strstr( ) function: locate a string within a string.
#include <stdio.h>
#include <string.h>
int main( )
{
char *s1="There is always something you miss.";
char *s2="way";
printf("%s\n",strstr(s1,s2));
return (0);
}
strupr( ) function: convert lowercase letters to uppercase.
#include <stdio.h>
#include <string.h>
char s1[]="Uppercase characters are easier to read.";
char *s2;
int main( )
{
s2=strupr(s1);
printf("The results: %s",s2);
return (0);
}
uses strncmp to compare two strings with the aid of the strlen function
#include <stdio.h>
#include <string.h>
main( )
{
char s1[]="Adam", s2[]="Abel";
int istringA_length,iresult=0;
istringA_length=strlen(s1);
if (strlen(s2) >= strlen(s1))
iresult = strncmp(s1,s2,istringA_length);
printf("The string %s found", iresult == 0 ? "was" : "wasn"t");
return(0);
}
Using memchr
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char *s = "This is a string";
cout << "The remainder of s after character "r" "
<< "is found is \"" << (char *) memchr( s, "r", 16 )
<< "\"" << endl;
return 0;
}
Using memcmp
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
int main()
{
char s1[] = "ABCDEFG", s2[] = "ABCDXYZ";
cout << "s1 = " << s1 << "\ns2 = " << s2 << endl
<< "\nmemcmp(s1, s2, 4) = " << setw( 3 )
<< memcmp( s1, s2, 4 ) << "\nmemcmp(s1, s2, 7) = "
<< setw( 3 ) << memcmp( s1, s2, 7 )
<< "\nmemcmp(s2, s1, 7) = " << setw( 3 )
<< memcmp( s2, s1, 7 ) << endl;
return 0;
}
Using memcpy
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char s1[ 17 ], s2[] = "Copy this string";
memcpy( s1, s2, 17 );
cout << "After s2 is copied into s1 with memcpy,\n"
<< "s1 contains \"" << s1 << "\"" << endl;
return 0;
}
Using memmove
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char x[] = "this is a test";
cout << "The string in array x before memmove is: " << x;
cout << "\nThe string in array x after memmove is: "
<< (char *) memmove( x, &x[ 5 ], 10 ) << endl;
return 0;
}
Using memset
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char string1[ 15 ] = "BBBBBBBBBBBBBB";
cout << "string1 = " << string1 << endl;
cout << "string1 after memset = "
<< (char *) memset( string1, "b", 7 ) << endl;
return 0;
}
Using strerror
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
cout << strerror( 2 ) << endl;
return 0;
}
Using strpbrk
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char *string1 = "This is a test";
char *string2 = "beware";
cout << "Of the characters in \"" << string2 << "\"\n""
<< *strpbrk( string1, string2 ) << "\""
<< " is the first character to appear in\n\""
<< string1 << "\"" << endl;
return 0;
}
Using strspn:
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char *string1 = "The value is 3.14159";
char *string2 = "test ";
cout << "string1 = " << string1
<< "\nstring2 = " << string2
<< "\n\nThe length of the initial segment of string1\n"
<< "containing only characters from string2 = "
<< strspn( string1, string2 ) << endl;
return 0;
}
using the strcpy function
#include <stdio.h>
#include <string.h>
#define iSIZE 20
main( )
{
char s[iSIZE]="Initialized String!",d[iSIZE];
strcpy(d,"String Constant");
printf("%s\n",d);
strcpy(d,s);
printf("%s\n",d);
return(0);
}