Escape characters and printf
Posted on August 13, 2008
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Escape Sequence
Character represented
\n
newline
\t
tab
\\
backslash itself
\’
quote ``’’’
\”
double quote ``”’’
For example,
printf("\\t\\"My name is \\'%s\\'\\"\\n", "Jim");
```Which prints
“My name is ‘Jim’”
I/O in c without printf and scanf.
Posted on August 13, 2008
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Problem
I/O in c without printf and scanf.
Method 1 - getchar and putchar
int getchar();
int putchar(int value);
Sample Program
#include <stdio .h> int main(void) { int c; while ((c = getchar()) != EOF) { putchar(c); } return 0; } Note the use of parentheses. c=gethar()!=EOF implies whether c = 0 or 1 depending on whether character get is not EOF or is EOF. EOF is 257 , so use int rather than char.
[Read More]C preprocessor
Posted on August 8, 2008
(Last modified on August 7, 2020)
| 15 minutes
| Kinshuk Chandra
The C preprocessor (cpp) is the preprocessor for the C programming language. In many C implementations, it is a separate program invoked by the compiler as the first part of translation. The preprocessor handles directives for source file inclusion (#include), macro definitions (#define), and conditional inclusion (#if). The language of preprocessor directives is not strictly specific to the grammar of C, so the C preprocessor can also be used independently to process other types of files.
[Read More]Complicated Declaration
Posted on August 7, 2008
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Function Declaration
Function
Returning
Accepting
int *f();
int ptr
any number of arg in c and no arg in cpp
char(*(*f())[10])();
pointer to the array of pointers to function
int (*(*f())[10]) ()
pointer to the array of 10 pointers to function that returns integer
Pointer
Expression
p is the pointer to
int *p;
iint **p;
int (*p)();
function that returns the integer
int (*p)[30]
array of 30 int
Program string class
Posted on August 7, 2008
(Last modified on August 7, 2020)
| 8 minutes
| Kinshuk Chandra
Program String Class(Source Code)
// Program Ch03pr06
// Program string class
#include
#include
#include
class string
{
private :
char *str ;
public :
string( ) ;
string ( char *s ) ;
string ( string& s ) ;
int length( ) ;
char operator [] ( int index ) ;
void operator = ( string s ) ;
string operator + ( string s ) ;
[Read More]Pattern matching program using Brute Force Algorithm
Posted on August 7, 2008
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Pattern matching program using Brute Force Algorithm(Source Code)
// Program Example Ch03pr05
// Pattern matching program using Brute Force Algorithm
#include
#include
#include
const int MAX = 20 ;
class string
{
private :
char str[MAX] ;
public :
string( ) ;
string ( char *s ) ;
static int xstrsearch ( string &s1, string &s2 ) ;
void show( ) ;
} ;
// initializes data
[Read More]Program to manage array of pointers to strings
Posted on August 7, 2008
(Last modified on August 7, 2020)
| 1 minutes
| Kinshuk Chandra
Program to manage array of pointers to strings (Source Code)
// Program Ch03pr04
// Program to manage array of pointers to strings
#include
#include
#include
const int MAX1 = 6 ;
const int MAX2 = 10 ;
class string
{
private :
char *names[MAX1] ;
int count ;
public :
string( ) ;
int add( ) ;
void show( ) ;
~string( ) ;
} ;
// allocates the memory
[Read More]Program with the use of array of pointers to strings
Posted on August 7, 2008
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Program with the use of array of pointers to strings(Source Code)
// Program: Ch03pr03
// Program with the use of array of pointers to strings
#include
#include
#include
const int MAX = 6 ;
class string
{
private :
char *names[MAX] ;
int count ;
public :
string( ) ;
int add ( char *s ) ;
void swap ( int i, int j ) ;
void show( ) ;
[Read More]Program to manage a 2-D array of Characters
Posted on August 7, 2008
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Program to manage a 2-D array of Characters(Source Code)
// Program Ch03pr02
// Program to manage a 2-D array
#include
#include
const int MAX1 = 6 ;
const int MAX2 = 10 ;
class string
{
private :
char masterlist[MAX1][MAX2] ;
int count ;
public :
string( ) ;
int add ( char *s ) ;
int find ( char *s ) ;
} ;
// initialises the data
[Read More]Making a String class in cpp
Posted on August 7, 2008
(Last modified on August 7, 2020)
| 2 minutes
| Kinshuk Chandra
Program to manage strings(Source Code)
// Program Ch03pr01
// Program to manage strings
#include <string.h\> #include <iostream.h\> #include <conio.h\> const int MAX \= 10 ; class string { private : char str\[MAX\] ; public : string( ) {} string ( char \*s ) {strcpy(str,s);} int xstrlen( ) ; static void xstrcpy ( string &t, string &s ) ; static void xstrcat ( string &t, string &s ) ; static int xstrcmp ( string &s1, string &s2 ) ; void show( ) ; } ; // finds the length of the string int string::xstrlen( ) { int l \= 0 ; char \*s \= str ; while ( \*s ) { l++ ; s++ ; } return l ; } // copies source string s to the target string t void string :: xstrcpy ( string &t, string &s ) { char \*s1 \= t.
[Read More]