Presentation is loading. Please wait.

Presentation is loading. Please wait.

One-dimensional character Arrays C does not have string data type. So strings are represented as arrays of characters. The end of the string is marked.

Similar presentations


Presentation on theme: "One-dimensional character Arrays C does not have string data type. So strings are represented as arrays of characters. The end of the string is marked."— Presentation transcript:

1 One-dimensional character Arrays C does not have string data type. So strings are represented as arrays of characters. The end of the string is marked as a special character known as null character (a character whose bits are zero, named as NUL in ASCII). Represented as ‘\0’. But C allows string constants. Ex: “Students”

2 One-dimensional character Arrays Declaration String can be represented as one-dimensional arrays like char name[30]; Initialization character arrays allow a shorthand initialization. Ex : char s[17] = “I love chocolate”; Similarly char s[17]={‘I’,’ ‘,’l’,’o’,’v’,’e’,’ ‘, ’c’,’h’,’o’,’c’,’o’,’l’,’a’,’t’,’e’} can also be written.

3 One-dimensional character Arrays Initialization Alternatively C allows the declaration like char str[]=“Good morning” ; Here the size of the string is 13 bytes.(12 for letters and one for terminating NUL) Printing String –The conversion type ‘s’ is used to print strings. –Width and precision specifications may be used with %s conversion specifier. Ex : printf(“%8.6s”,str); Whose Output will be “ Good m” But printf(“%-8.6s”,str); will give “Good m “ where the minus sign will make it left justified.

4 One-dimensional character Arrays Rules When the field width is less than the string then the entire string is printed. The integer value on the right side of the decimal point indicates the no of characters to be printed. When number of characters to be printed is taken as zero, nothing is printed. The minus sign in the specification makes the string to be printed with left justification.

5 One-dimensional character Arrays Printing String The library function puts() can also be used to print the strings. This function terminates the line with a new line,’\n’. It returns an EOF if an error occurs and returns a positive number upon success. The library function sprintf(), similar to printf() is used to construct formatted strings in memory and to transmit over a communication channel or to a special device. The library function puts() may be used to copy a string to the standard output, whose single parameter is the start address of the string.

6 One-dimensional character Arrays Printing String Ex : void main() { char buff[100]; float x=1.2345; int I; sprintf(buff,”x=%f”,x); for(i=0;i<=5;i++) puts(buff+i); } will give the output like x=1.234500 =1.234500 1.234500.234500 234500 34500 Here the puts() function puts a new-line every time.

7 One-dimensional character Arrays String input Strings can be read by using the %s conversion specifier with scanf() function. Scanf() only recognizes a sequence of characters delimited by white space as an external string. It is the job of the programmer to ensure that there is enough memory space to receive and store the incoming string along with the terminating null, which is generated automatically and is stored by scanf(). void main() { char str[30]; scanf(“%s”,str);. printf(“str=%s”,str); if input is intellectual output will be intellectual if input is anu acharya output will be anu. if input is “anuradha” output will be “anuradha”.

8 One-dimensional character Arrays String input C supports variable field width and precision. Ex : printf(“%*.*s”,w,d,str); Thus printf(“%*.*s”,3,4,str); if str is anuradha then output will be anur. Using Scanset A scanset conversion consists of a list of acceptable characters enclosed within square brackets like [a-z]. If ‘-’ is required actually it must be the first or the last character in the list. If the first character after ‘[‘ is ‘^’ then rest of the scanset specifies unacceptable characters.

9 One-dimensional character Arrays Using Scanset Ex : scanf(“%[a-z]”,str); if input value is hello Manas then output will be hello manas only. But if input is manas1234 output will be manas only. Single-line input using scanset with ^ If we want to terminate the string when ‘\n’ is entered then we have to write scanf(“%[^\n]”,str); Multiline input using scanset If the string is to be terminated only when ‘+’ is entered then it is required to write scanf(“%[^+]”,str); Here the ‘+’ is not included in the input stream will be stored in the buffer and will be taken as the input for the next scanf() automatically. Both %s and %[ ] automatically generate the NUL.

10 One-dimensional character Arrays Scanf() with conversion specifier %c Ex : char str[10]; for(i=0;i<=4;i++) {scanf(“%10c”,str); str[9]=‘\0’; printf(“%s”,str);} input output 123456789 123456789 abcdefghi abcdefghi abcdefghijklmnopqrabcdefghi 123456789 klmnopqr ppppppppp23456789

11 One-dimensional character Arrays gets() function Reads the complete input line and is stored in the memory area as a null-terminated string. Ex : gets(str); input : Belive in GOD Sscanf() function This function applies scanf() type conversions to data held in a buffer as a single string but will nor read data from standard input. Ex : char inbuf[100]; int num; gets(inbuf); sscanf(inbuf,”%d”,&num); printf(“num = %d”,num); input = 1234 output = 1234

12 One-dimensional character Arrays atoi() function This function takes the address of an area of memory and converts the string stored at that location to an integer. Sscanf() can do all possible conversions where as atoi() can only do single decimal integer conversions. In the above example the same thing can be done by num=atoi(inbuf);

13 One-dimensional character Arrays fscanf() and fprintf() There are three I/O streams. They are stdin,stdout and stderr. fprintf() sends formatted output to a stream and fscanf() scans and formats input from a stream. Ex : int x,y; fprintf(stdout,”Enter values for x and y : “); fscanf(stdin,”%d %d”,&x,&y); fprintf(stdout,”x = %d y = %d”,x,y); input :- Enter values for x and y : 2 3 output :- x = 2 y = 3

14 One-dimensional character Arrays Similarly the output can also be send to stderr like fprintf(stderr,”Error in the input”); Note : If we write int i =‘1’; i will not store 1 but will store the ascii equivalent of the character ‘1’ which is 49. Thus irrespective of the values of ‘1’ and ‘0’ ‘1’-’1’=0 and ‘0’-’0’=0 are true. Or in general c-’0’ gives its value,where c is a variable holding some digit character. So “245” is not the integer 245. It can be converted into the corresponding integer by calling atoi() function like int i; i=atoi(“245”);


Download ppt "One-dimensional character Arrays C does not have string data type. So strings are represented as arrays of characters. The end of the string is marked."

Similar presentations


Ads by Google