String in c language androwep

String in c language androwep

The simplest of the console I/O functions are getchar( ), which reads a character from the keyboard, and putchar( ), which writes a character to the screen. The getchar( ) function waits until a key is pressed and then returns its value. The keypress is also automatically echoed to the screen. The putchar( ) function writes a character to the screen at the current cursor position.

#include <stdio.h> 
#include <ctype.h> 
int main(void) {   
  char ch;
  printf("Enter some text (type a period to quit).\n");  
  do {     
       ch = getchar();
       if(islower(ch)) 
           ch = toupper(ch);    
       else 
           ch = tolower(ch);
       putchar(ch);
     } while (ch != '.');
  return 0;
 }

A Problem with getchar( )

There are some potential problems with getchar( ). For many compilers, getchar( ) is implemented in such a way that it buffers input until ENTER is pressed. This is called line-buffered input; you have to press ENTER before any character is returned. Also, since getchar( ) inputs only one character each time it is called, line buffering may leave one or more characters waiting in the input queue, which is annoying in interactive environments.

For most compilers, the prototypes for these functions are found in the header file . For some compilers, these functions have a leading underscore. For example, in Microsoft’s Visual C++, they are called _getch( ) and _getche( ).The getch( ) function waits for a keypress after which it returns immediately. It does not echo the character to the screen. The getche( ) function is the same as getch( ), but the key is echoed. You will frequently see getche( ) or getch( ) used instead of getchar( ) when a character needs to be read from the keyboard in an interactive program. For example, the previous program is shown here using getch( ) instead of getchar( ):

#include <stdio.h> 
#include <conio.h> 
#include <ctype.h> 
int main(void) {  
   char ch;
   printf("Enter some text (type a period to quit).\n"); 
   do {     
       ch = getch();
       if(islower(ch)) 
           ch = toupper(ch);    
       else 
           ch = tolower(ch);
       putchar(ch);  
      } while (ch != '.');
 return 0;
 }

NOTE : At the time of this writing, when using Microsoft’s Visual C++ compiler, _getche( ) and _getch( ) are not compatible with the standard C input functions, such as scanf ( ) or gets( ). Instead, you must use special versions of the standard functions, such as cscanf( ) or cgets( ). You will need to examine the Visual C++ documentation for details.

Reading and Writing Strings

C language have more function for string. When we are using string function before we are use #include <string.h> header file. like as

1)strlen(string_name)returns the length of string name.
2)strcpy(destination, source)copies the contents of source string to destination string.
3)strcat(first_string, second_string)concats or joins first string with second string. The result of the string is stored in first string.
4)strcmp(first_string, second_string)compares the first string with second string. If both strings are same, it returns 0.
5)strrev(string)returns reverse string.
6)strlwr(string)returns string characters in lowercase.
7)strupr(string)returns string characters in uppercase.

What is C string?

string in C (also known as C string) is an array of characters, followed by a NULL character. To represent a string, a set of characters are enclosed within double quotes (“).

Why do we use string in C?

Unlike arrays we do not need to print a string, character by character. The C language does not provide an inbuilt data type for strings but it has an access specifier “%s” which can be used to directly print and read strings. You can see in the above program that string can also be read using a single scanf statement.

What is the function of string in C?

Commonly used String functions in C/C++ with Examples. Strings in CStrings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’. It will append copy of the source string in the destination string.