CSE1320 Strings Dr. Sajib Datta CSE@UTA.

Slides:



Advertisements
Similar presentations
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Advertisements

What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Strings CS240 Dick Steflik. What is a string A null terminated array of characters: char thisIsAString[10]; \0 The “\0” (null character)
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
ECE Application Programming Instructor: Dr. Michael Geiger Spring 2012 Lecture 31: PE5.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to C Programming CE Lecture 13 Strings in C.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
POINTER Dong-Chul Kim BioMeCIS UTA 3/13/
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
CSE 251 Dr. Charles B. Owen Programming in C1 Strings and File I/O.
Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.
Dr. Sajib Datta  Ordering elements in some way  For numeric data, ascending order is the most common  Lots of techniques for sorting  These.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
Buffer Overflow By Collin Donaldson.
LINKED LISTS.
INC 161 , CPE 100 Computer Programming
Functions and Pointers
ECE Application Programming
Characters and Strings
Fundamentals of Characters and Strings
ICS103 Programming in C Lecture 15: Strings
DEPARTMENT OF COMPUTER SCIENCE & APPLICATION
Lecture 8 String 1. Concept of strings String and pointers
CSE 1320 Search, Sort and Strings
CSE1320 Loop Dr. Sajib Datta
Functions Dr. Sajib Datta
CSE 303 Lecture 14 Strings in C
Module 2 Arrays and strings – example programs.
Exercises on String Operations
Strings A string is a sequence of characters treated as a group
Arrays in C.
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Character Strings Lesson Outline
Functions and Pointers
CSCI206 - Computer Organization & Programming
Computer Science 210 Computer Organization
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
Pointers.
C Stuff CS 2308.
Strings.
CSI 121 Structured Programming Language Lecture 21 Character Strings
Engr 0012 (04-1) LecNotes
INC 161 , CPE 100 Computer Programming
CSE1320 Strings Dr. Sajib Datta
C Characters and Strings – Review Lab assignments
Lecture 11 Strings.
Beginning C for Engineers
C-strings In general, a string is a series of characters treated as a unit. Practically all string implementations treat a string as a variable-length.
Strings.
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
EECE.2160 ECE Application Programming
Line at a time I/O with fgets() and fputs()
ICS103 Programming in C Lecture 15: Strings
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
String manipulation string.h library
POINTER CONCEPT 4/15/2019.
EECE.2160 ECE Application Programming
Character Arrays char string1[] = “first”;
Strings #include <stdio.h>
Programming Strings.
POINTER CONCEPT 8/3/2019.
Introduction to Problem Solving and Programming
Presentation transcript:

CSE1320 Strings Dr. Sajib Datta CSE@UTA

String functions strcat() strcpy() strtok()

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.

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.

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

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; }

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; }

Output copy - Be the beast orig - beast

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); }

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"); }

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," ."); }

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," ."); }

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); }