1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <cstdlib>
#include <iostream>
#include <string.h>
using namespace std;
void strcpy(void);
void strncpy(void);
void strcmp(void);
void strncmp(void);
void strcat(void);
void strncat(void);
void strlen(void);
int main()
{
int w=1, choice;
do
{
cout<<"You have entered the string demostration program."<<endl;
cout<<"Please enter 1 for strcpy, 2 for strncpy, 3 for strcmp, 4 for"<<endl<<"strncmp, 5 for strcat, 6 for strncat, or 7 for strlen>> ";
cin>>choice;
switch(choice)
{
case 1:
{
strcpy();
}
case 2:
{
strncpy();
}
case 3:
{
strcmp();
}
case 4:
{
strncmp();
}
case 5:
{
strcat();
}
case 6:
{
strncat();
}
case 7:
{
strlen();
}
default:
{
cout<<"Please enter 1, 2, 3, 4, 5, 6, or 7."<<endl;
}
do
{
cout<<"Enter 1 to continue or 2 to quit";
cin>>w;
}while((w!=1)&&(w!=2));
}while((w==1)||(w!=2));
system("PAUSE");
return EXIT_SUCCESS;
}
void strcpy(void)
{
char str1[]="Sample string";
char str2[40];
char str3[40];
cout<<"This function gives an example of strcpy."<<endl;
cout<<"strcpy puts characters and integers into arrays in the iostream"<<endl;
strcpy (str2,str1);
strcpy (str3,"copy successful");
printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3);
}
void strncpy(void)
{
char str1[]= "To be or not to be";
char str2[40];
char str3[40];
cout<<"This function gives an example of strncpy."<<endl;
cout<<"strncpy takes the first number for the source(array)and pushes it"<<endl<<"to the destination. If it finds a null character before a number"<<endl<<"it will copy a zero."<<endl;
strncpy ( str2, str1, sizeof(str2) );
strncpy ( str3, str2, 5 );
str3[5] = '\0';
puts (str1);
puts (str2);
}
void strcmp(void)
{
char szKey[] = "apple\n";
char szInput[80];
cout<<"This function gives an example of strcmp."<<endl;
cout<<"strcmp compares the first character of each string. If they are "<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ or until a terminating null-character is found"<<endl;
do
{
printf ("Guess my favorite fruit? ");
fgets (szInput,80,stdin);
} while (strcmp (szKey,szInput) != 0);
cout<<"Correct."
}
void strncmp(void)
{
char str[][5] = { "R2D2" , "C3PO" , "R2A6" };
int n;
cout<<"This function gives an example of strncmp."<<endl;
cout<<"strncmp compares the first character of each string. If they are"<<endl<<"equal to each other, it continues with the following pairs until"<<endl<<"the characters differ, until a terminating null-character is "<<endl<<"reached, or until num characters match in both strings, whichever happens first.
cout<<"Looking for R2 astromech droids..."
for (n=0 ; n<3 ; n++)
{
if (strncmp (str[n],"R2xx",2) == 0)
{
printf ("found %s\n",str[n]);
}
}
}
void strcat(void)
{
char str[80];
cout<<"This function gives an example of strcat."<<endl;
cout<<"strcat adds a copy of the source to the destination. If the source"<<endl<<"was and the destination was after strcat the "<<endl<<"destination would read with a terminating null character.
strcpy (str,"these ");
strcat (str,"strings ");
strcat (str,"are ");
strcat (str,"concatenated.");
puts (str);
}
void strncat(void)
{
char str1[20];
char str2[20];
cout<<"This function gives an example of strncat."<<endl;
strcpy (str1,"To be ");
strcpy (str2,"or not to be");
strncat (str1, str2, 6);
puts (str1);
}
void strlen(void)
{
char szInput[256];
cout<<"This function gives an example of strlen."<<endl;
cout<<"strlen determines the length of a string by counting from the"<<endl<<"forst character to the terminating null character (count does not "<<endl<<"include the terminating character."<<endl;
printf ("Enter a sentence: ");
gets (szInput);
printf ("The sentence entered is %u characters long.\n",(unsigned)strlen(szInput));
}
}
| |