Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of.

Similar presentations


Presentation on theme: "1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of."— Presentation transcript:

1 1 CS 201 String Debzani Deb

2 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of the two strings. However, a type char value is not a valid argument for a function with a corresponding parameter of type char *. If you wish to concatenate a single character, you must use “a” and not ‘a’.

3 3 Distinction Between Characters and Strings #include int main(){ char str[20]; strcpy(str,"Hello"); strcat(str,"World"); printf ( "%s", str); return 0; } #include int main(){ char str[20]; strcpy(str,"Hello"); strcat(str, 'K'); printf ( "%s", str); return 0; }

4 4 Scanning a Full Line scanf only gets non-whitespace characters, but sometimes it is necessary to get the whitespace characters. Inside the stdio library there is a function gets and fgets. char read_line[80]; gets(read_line);  The first line declares a string of size 80, the second will get 80 characters from the user.  However, if the user enters more than 80 characters we will get an overflow. fgets is similar, but it takes three arguments. char read_line[80]; fgets(file, read_line, size);

5 5 Character Operations We can put #include at the top of our program and we have more functions that can be used to operate on characters. We can use scanf(”%c”, &ch); or ch = getchar(); Also we can use fscanf(inp, ”%c”, &ch); or ch = getc(inp); to get characters from a file. We can use printf(”%c”, ch); or putchar(ch); to output a character to the screen Also we can use fprintf(outp, ”%c”, ch); or putc(ch, outp);

6 6 Character Analysis and Conversion All the following functions return non-zero value if true.  isalpha(ch) - checks to see if the variable ch is holding a character value  isdigit(ch) - checks to see if the variable ch is holding a digit and returns 0 for no and a positive integer for yes  islower(ch) - checks to see if ch is holding a lowercase character  isupper(ch) - checks to see if ch is holding an uppercase character  ispunct(ch) - this checks to see if ch is holding a punctuation character  isspace(ch) - this checks to see if ch is holding a space

7 7 String-to-Number and Number-to-String Conversions sprintf takes numbers, doubles, characters, and strings and concatenates them into one large string. sprintf(string_1, ”%d integer %c - %s”, int_val, char_val, string_2);  If int_val = 42, char_val = ’a’, and string_2 = ”Hello”, then: string_1 would be “42 integer a - Hello”. sscanf takes a string and tries to parse it into integer, doubles, characters, and strings.  sscanf(“42 3.141592 Test1 Test2”, “%d %lf %s %s”, &num, &pi, &string 1, &string 2);  Now num has the value 42, pi has the value 3.141592, string_1 has the value ”Test1”, and string_2 has the value ”Test2”.


Download ppt "1 CS 201 String Debzani Deb. 2 Distinction Between Characters and Strings When using strcat, one may be tempted to supply a single character as one of."

Similar presentations


Ads by Google