Presentation is loading. Please wait.

Presentation is loading. Please wait.

INC 161 , CPE 100 Computer Programming

Similar presentations


Presentation on theme: "INC 161 , CPE 100 Computer Programming"— Presentation transcript:

1 INC 161 , CPE 100 Computer Programming
Lecture 7 String

2 Characters Using char to declare a character variable.
Characters such as letters and punctuation are stored as integers according to a certain numerical code, such as ASCII code that ranges from 0 to 127 and only requires 7 bits to represent it. Character constant is an integral value represented as a character in single quotes. ’a’ represents the integer value of a. If ASCII code is used to represent characters, the ASCII value for ’a’ is 97. Using char to declare a character variable. char c = ’a’; // the same as char c = 97;

3 Character Input and Output
Functions in <stdio.h> int getchar(void); Reads the next character from the standard input. int putchar(int c); Print the character stored in c int scanf(const char *, …); with conversion specifier c. char c; scanf(“%c”, &c); int printf(char *, …); char c=’a’; printf(“%c”, c);

4 Strings Strings are series of characters treated as a single unit
Can include letters, digits, and certain special characters (*, /, $) String literal (string constant) - a sequence of multi-byte characters enclosed in double quotes "Hello" Declare a string as a character array or a variable of type char * char str1[7] = {’S’, ’t’, ’r’, ’i’, ’n’, ’g’, ’\0’}; char str2[ ] = {’S’, ’t’, ’r’, ’i’, ’n’, ’g’, ’\0’}; char str3[ ] = “String”; char *strPtr = “String”; String must have an end character 0 or ‘\0’ or NULL

5 char a[10] = “Hello” ‘H’ ‘e’ ‘l’ ‘l’ ‘o’ ‘\0’
Don’t care what it stores End Character

6 String Input and Output
Functions in <stdio.h>

7 Example 2 : sprintf, sscanf
#include <stdio.h> main() { char str[40]; int a = 123, b = 0; printf("Please input a string.\n"); scanf(“%s”,str); printf(“The input is: ”); printf(“%s”,str); sprintf(str,“The value of a %d\n”,a); sscanf(str,“The value of a %d\n”,&b); printf(“After sscanf b is %d\n”,b); }

8 String Manipulation Header file string.h has functions to
Determining string length strlen Copying strings strcpy Appending strings strcat Comparing strings strcmp Searching strings Tokenizing strings

9 Copying strings Function Description
char *strcpy(char *s1, const char *s2) Copies string s2 into s1. The value of s1 is returned. char *strncpy(char *s1, const char *s2, size_t n) Copies at most n characters of string s2 into s1. If there are fewer than n characters in s2 before the null terminating character, then null characters are added into s1 as padding until the specified number has been written.The value of s1 is returned.

10 Comparing strings Function Description
int strcmp(const char *s1, const char *s2) Compare string s1 to s2. int strncmp(const char *s1, const char *s2, size_t n) Compare up to n characters of string s1 to s2.

11 Example 3: strcmp, strcpy, strcat
#include <stdio.h> #include <string.h> main() { char s1[10]="ABCD", s2[10]="ABCD", s3[10]="abcd"; printf("strcmp(s1, s2) = %d\n", strcmp(s1, s2)); printf("strcmp(s1, s3) = %d\n", strcmp(s1, s3)); printf("strcmp(s3, s1) = %d\n", strcmp(s3, s1)); strcpy(s1, s3); printf("s1 = %s\n", s1); strcpy(s1, "1234"); strcat(s1, "Hello"); printf("strlen(s1) = %d\n", strlen(s1)); }

12 Output: strcmp(s1, s3) = -1 strcmp(s3, s1) = 1 s1 = abcd s1 = 1234
s1 = 1234Hello strlen(s1) = 9


Download ppt "INC 161 , CPE 100 Computer Programming"

Similar presentations


Ads by Google