Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE1320 Strings Dr. Sajib Datta CSE@UTA.

Similar presentations


Presentation on theme: "CSE1320 Strings Dr. Sajib Datta CSE@UTA."— Presentation transcript:

1 CSE1320 Strings Dr. Sajib Datta

2 String functions strcat() strcpy() strtok()

3 strcat() strcat() for string concatenation
Take two strings for arguments A copy of the second string is tacked onto the end of the first, and this combined version becomes the new first string. The second string is not altered. The return type of strcat () is char *, the value of its first argument – the address of the first character of the string to which the second string is appended.

4 strcat() cont. #include <stdio.h> #include <string.h>
int main(void) { char flower[80]; char addon[] = " is a cat."; scanf("%s", flower); strcat(flower, addon); printf("%s\n",flower); return 0; } Note!!! strcat( ) not checking whether the first array is large enough to hold the second string.

5 If your input is Kitty, your output is going to be as follow.
Kitty is a cat.

6 strcpy() #include <stdio.h> #include <string.h>
int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; strcpy(copy, orig); printf("copy - %s\n", copy); printf(“orig - %s\n", orig); return 0; }

7 strcpy() #include <stdio.h> #include <string.h>
int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; strcpy(copy+7, orig); printf("copy - %s\n", copy); printf(“orig - %s\n", orig); return 0; }

8 Output copy - Be the beast orig - beast

9 strtok() #include<stdio.h> #include<string.h> void main(){ char sentence[]="Bill is really cool."; char *word = strtok(sentence," ."); printf("%s\n",word); char *secondword = strtok(NULL," ."); printf("%s\n",secondword); }

10 strtok() cont. #include<stdio.h> #include<string.h> void main(){ char sentence[]="Bill is really cool."; char *word = strtok(sentence,"xz"); printf("%s\n",word); char *secondword = strtok(NULL,"xz"); if(secondword==NULL) printf(":)\n"); }

11 strtok() cont. #include<stdio.h> #include<string.h> void main(){ char sentence[]="Bill is really cool."; char *word = strtok(sentence," ."); while(word!=NULL) { printf("%s\n",word); word = strtok(NULL," ."); }

12 strtok() cont. Is there anything wrong in this code?
#include<stdio.h> #include<string.h> void main(){ char *sentence="Bill is really cool."; char *word = strtok(sentence," ."); while(word!=NULL) { printf("%s\n",word); word = strtok(NULL," ."); }

13 Use fgets() Not gets() #include <stdio.h> int main() { char name[10]; printf("What’s your name? \n"); fgets(name,10,stdin); printf(“Nice to meet you, %s.\n",name); return(0); }

14 fopen() and fclose() fopen() function is used to open a file to perform operations such as reading, writing etc. FILE *fp; fp=fopen (“filename”, ”‘mode”); fclose() function closes the file that is being pointed by file pointer fp. fclose(fp);

15 # include <stdio.h>
# include <string.h> int main( ) { FILE *fp ; char data[50]; // opening an existing file printf( "Opening the file test.c in write mode" ) ; fp = fopen("test.c", "w") ; printf( "\n Enter some text to write to the file test.c" ) ; // getting input from user while ( strlen ( gets( data ) ) > 0 ) // writing in the file fputs(data, fp) ; fputs("\n", fp) ; } // closing the file printf("Closing the file test.c") ; fclose(fp) ; return 0;

16 Output: > gcc  file1.c –o file > ./file  Opening the file test.c in write mode Enter some text to write to the file test.c Hello World!!! Closing the file test.c

17 File Operations A program has many ways to obtain input data, for example  using scanf to get data from a user through keyboard. using argc and argv to get data from the command line. using file operations to get data stored on a disk.

18 The following example shows the usage of fscanf() function.
#include <stdio.h> #include <stdlib.h> int main () { char str1[10], str2[10], str3[10]; int year; FILE * fp; fp = fopen ("file.txt", "w+"); fputs("We are in 2018", fp); rewind(fp); fscanf(fp, "%s %s %s %d", str1, str2, str3, &year); printf("Read String1 |%s|\n", str1 ); printf("Read String2 |%s|\n", str2 ); printf("Read String3 |%s|\n", str3 ); printf("Read Integer |%d|\n", year ); fclose(fp); return(0); }

19 Output Read String1|We| Read String2 |are| Read String3 |in| Read Integer |2018|

20 The following program using argv[1] as a file’s name
The following program using argv[1] as a file’s name. This programs checks whether argc is at least two to decide whether argv[1] is available. #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { if (argc < 2) printf("Need to provide the file's name.\n"); return EXIT_FAILURE; } printf("The name of the file is %s.\n", argv[1]); return EXIT_SUCCESS;

21 Running the program without the file’s name will produce a message and the program returns EXIT FAILURE. Output: > gcc file1.c -o file1 > ./file1 Need to provide the file’s name. If the file’s name is given, the program prints the file’s name: > ./file1 data The name of the file is data.

22 Read Character by Character Using fgetc
Read Character by Character Using fgetc. This program counts the number of characters in the input file. #include <stdio.h> #include <stdlib.h> int main(int argc, char * argv[]) { FILE * fptr; int ch; int counter = 0; fptr = fopen(argv[1], "r"); printf("The name of the file is %s.\n", argv[1]); do ch = fgetc(fptr); if (ch != EOF) counter ++; } } while (ch != EOF); fclose(fptr); printf("The file has %d characters.\n", counter); return EXIT_SUCCESS;

23 Output: > gcc  file2.c -o file2 > ./file2 file2.c The name of the file is file2.c The file has 716 characters.

24 A sample program to read input from a file and store the output to an another file.
#include <stdio.h> #include <stdlib.h> // For exit() function int main() { char c[1000]; FILE *fptr; if ((fptr = fopen(“program.txt”, “r”)) == NULL) printf("Error opening file!"); exit(1); } // reads text until newline fscanf(fptr,"%[^\n]", c); printf("Data from the file:\n%s", c); fclose(fptr); return 0;

25 > cat program.txt Welcome to Intermediate Programming Course We are learning I/O and files. Have fun coding! Output > gcc file.c -o file > ./file >> output.txt > cat output.txt Data from the file:

26 A sample C program to create a file and write data into file.
#include <stdio.h> #include <stdlib.h> #define DATA_SIZE 1000 int main() { /* Variable to store user content */ char data[DATA_SIZE]; /* File pointer to hold reference to our file */ FILE * fptr; /* Open file in w (write) mode.*/ fptr = fopen("file1.txt", "w"); if(fptr == NULL) /* File not created hence exit */ printf("Unable to create file.\n"); exit(EXIT_FAILURE); }

27 /* Input contents from user to store in file */
printf("Enter contents to store in file : \n"); fgets(data, DATA_SIZE, stdin); /* Write data to file */ fputs(data, fptr); /* Close file to save file data */ fclose(fptr); printf("File created and saved successfully. \n"); return 0; }

28 Output > gcc sample.c -o sample >./sample Enter contents to store in file :  Hello World! This is a sample text file File created and saved successfully. > vi file1.txt  This is a sample text file


Download ppt "CSE1320 Strings Dr. Sajib Datta CSE@UTA."

Similar presentations


Ads by Google