"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Program to demonstrate the 'strcmp' function'''\n",
"\n",
"#Variable declaration\n",
"string1 = \"Jerry\\0\" \n",
"string2 = \"Ferry\\0\"\n",
"\n",
"#Function definition\n",
"def strcmp (string1 , string2):\n",
" if (string1 == string2):\n",
" v = 0 #If the two strings are identical, strcmp returns a value zero\n",
" return v\n",
" else:\n",
" n = 0\n",
" while ( string1[n]):\n",
" if ( string1[n] == string2[n]):\n",
" n = n + 1\n",
" continue\n",
" else:\n",
" v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n",
" return v\n",
" return v\n",
"\n",
"#Function call\n",
"i = strcmp ( string1, \"Jerry\\0\" ) \n",
"j = strcmp ( string1, string2 ) \n",
"k = strcmp ( string1, \"Jerry boy\\0\" )\n",
"\n",
"#Result\n",
"print i,j,k"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"0 4 -32\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Two Dimensional Array of Characters, Page number: 344
"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"'''Program asks you to type your name. When you do so, it checks your\n",
"name against a master list to see if you are worthy of\n",
"entry to the palace'''\n",
"\n",
"\n",
"#Function definition\n",
"def strcmp (string1 , string2):\n",
" if (string1 == string2):\n",
" v = 0 #If the two strings are identical, strcmp returns a value zero\n",
" return v\n",
" else:\n",
" n = 0\n",
" while ( string1[n]):\n",
" if ( string1[n] == string2[n]):\n",
" n = n + 1\n",
" continue\n",
" else:\n",
" v = ord(string1[n]) - ord(string2[n]) #returns the numeric difference between the ASCII values of the first non-matching pairs of characters\n",
" return v\n",
" return v\n",
"\n",
"#Variable declaration\n",
"FOUND = 1\n",
"NOTFOUND = 0\n",
"masterlist =[\"akshay\",\"parag\",\"raman\",\"srinivas\",\"gopal\",\"rajesh\"]\n",
"yourname = []\n",
"flag = NOTFOUND\n",
"\n",
"#Input from user\n",
"#yourname = raw_input(\"Enter your name: \")\n",
"yourname = \"Akshatha\"\n",
"\n",
"#Checking in the master list\n",
"for i in range(0,6):\n",
" a = strcmp ( masterlist[i], yourname )\n",
" if a == 0:\n",
" print \"Welcome, you can enter the palace\" \n",
" flag = FOUND\n",
" break\n",
" \n",
"if flag == NOTFOUND :\n",
" print \"Sorry, you are a trespasser\" "
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Sorry, you are a trespasser\n"
]
}
],
"prompt_number": 10
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"
Exchange Names using 2-D Array of Characters, Page number: 348