click on this link......

LinkGrand.com

Wednesday 13 March 2013

Strings

String is a character array which is terminated by null character (\0). To use string in C program you have to declare character array first.
eg char str[10];
In above example, you can store string of 9 characters because last character is null character.
Some important functions related to string :-
1) strcpy(s1,s2) : It copies s2 into s1.
2) strcat(s1,s2) : Join s2 onto end of s1.
3) strlen(s1) : It returns length of s1 (integer value).
4) strcmp(s1,s2) : It compares two strings and returns -
            a) returns 0 if s1and s2 are same.
            b) returns negative if s1<s2
           c) returns positive if s1>s2.
5) strrev(s1) : It reverses the string.
6) strlwr(s1) : It will change a string to lower case.
7)strupr(s1) : It will change a string to upper case.

Console input Output function for reading or writing character and string:
1) getchar(): -
It reads the char from keyboard and wait for carriage return. The simple input mechanism to read one char at a time from stdio with getchar(). Every time getchar() is called it wait till a key is pressed and return its value. The getchar() returns an int value. This function can be used to read int and char type of data.

2) getche() :
It reads a char and does not wait for carriage return.

3) putchar() :
putchar() is used for output. It writes characters, arguments to the string at current cursor position. The prototype of putchar() defines it to have an int type arguments However it can be called with int or char type argument. In both cases it will find out the ASCII value for that argument and display it.

3) puts() : writes a string on screen. the puts() takes a string as its argument and writes it on screen The output is followed by new line This is because of null terminator placed at the end of the string.

4) gets() : gets() reads a string of char entered at the keyboard and places them at certain address char's can be type until, the carriage return its space which signal to gets() that the input is over However the carriage return does not returns apart of string.

Escape sequence:

Escape sequence can be used with printf() and they are mainly for screen formatting of output. They are always preceded with "\". (backslash)

\n -> New line.
\t -> horizontal tab
\b -> backspace
\0 -> Null

\v -> vertical tab
\' -> single quote
\" -> Double quotes



No comments:

Post a Comment