Exercises on String Operations

Slides:



Advertisements
Similar presentations
Array_strcpy void array_strcpy(char dest[], char src[]) { int i = 0; while (src[i] != '\0') { dest[i] = src[i]; i++; } dest[i] = '\0'; }
Advertisements

Sort the given string, without using string handling functions.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
N-1 University of Washington Computer Programming I Lecture 19: Strings © 2000 UW CSE.
Introduction to C Programming CE Lecture 13 Strings in C.
Declarations/Data Types/Statements. Assignments Due – Homework 1 Reading – Chapter 2 – Lab 1 – due Monday.
Exercise 7 Strings. An array of characters Used to store text Another way to initialize: char A[ ]=“blabla”;
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
Strings in C. Strings are Character Arrays Strings in C are simply arrays of characters. – Example:char s [10]; This is a ten (10) element array that.
Strlen() implementation /* strlen : return length of string s */ int strlen(char *s) { int n; for (n = 0 ; s[n] != ‘\0’ ; n++) ; return n; } /* strlen.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
CPT: Strings/ Computer Programming Techniques Semester 1, 1998 Objectives of these slides: –to discuss strings and their relationship.
CS140: Intro to CS An Overview of Programming in C (part 3) by Erin Chambers.
Lecture 1 cis208 January 14 rd, Compiling %> gcc helloworld.c returns a.out %> gcc –o helloworld helloworld.c returns helloworld.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
1 TOPICS TO DISCUSS : FUNCTIONS TYPES OF FUNCTIONS HEADER FILES PRESENTED BY : AVISHEK MAJUMDAR(837837) GUNJAN AGARWAL(856587) SATYAPRIYA DEY(856624)
IO revisited CSE 2451 Rong Shi. stdio.h Functions – printf – scanf(normally stops at whitespace) – fgets – sscanf Standard streams – stdin(defaults to.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
Tutorial #4 Summer 2005.
LINKED LISTS.
CS1010 Programming Methodology
Characters and Strings
Formatted Input/Output
Functions, Part 2 of 2 Topics Functions That Return a Value
Strings (מחרוזות).
Understand argc and argv
Chapter 2 Overview of C.
Plan for the Day: I/O (beyond scanf and printf)
Week 5 – Functions (1) EKT120: Computer Programming.
Strings A string is a sequence of characters treated as a group
Computer Science 210 Computer Organization
Formatted Input/Output
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Lecture 10: Strings B Burlingame 4 April 2018.
2008/11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park
More Examples of argc and argv
Computer Science 210 Computer Organization
Compiled and ready to run Memory Stack /*
CSE1320 Files in C Dr. Sajib Datta
Yung-Hsiang Lu Purdue University
توابع ورودي-خروجي.
Characters and Strings
INC 161 , CPE 100 Computer Programming
CSE1320 Strings Dr. Sajib Datta
CSCE 206 Lab Structured Programming in C
C Characters and Strings – Review Lab assignments
Functions, Part 2 of 3 Topics Functions That Return a Value
Introduction to Computer Organization & Systems
Line at a time I/O with fgets() and fputs()
ICS103 Programming in C Lecture 15: Strings
C Security Pre Function
ECE 103 Engineering Programming Chapter 51 Random Numbers
ECE 103 Engineering Programming Chapter 25 C Strings, Part 1
EECE.2160 ECE Application Programming
Introduction to Computer Organization & Systems
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Character Arrays char string1[] = “first”;
Strings #include <stdio.h>
Programming Strings.
Session 1 – Introduction to Computer and Algorithm (Part 2)‏
CSCE 206 Lab Structured Programming in C
Characters and Strings Functions
Strings Adapted from Dr. Mary Eberlein, UT Austin.
C Characters and Strings
CSCE 206 Lab Structured Programming in C
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Exercises on String Operations

Exercise 1: Write a program that inputs four strings that represent integers, converts the strings to integers, sums the values and prints the total of the four values. #include <stdio.h> #include <stdlib.h> int main(void){ char x[10]; char y[10]; char z[10]; char t[10]; int sum=0; puts("Enter 4 strings to be summed"); scanf(" %s %s %s %s", x, y, z, t); printf("\nSum is=%d", atoi(x)+atoi(y)+atoi(z)+atoi(t)); }

Exercise 2: Write a program that Counts the Number of Words in a String scanf with %s only considers one word scanf(“%s”, s); #include <stdio.h> #include <ctype.h> #include <string.h> int main(){ char s [40],ch; int i,c=0; printf("Enter any string : "); for(i=0;ch!='\n';i++){ scanf("%c",&ch); s[i]=ch; } s[i]='\0'; printf("%s", s); int len = strlen(s); for(i=0;i<len;i++){ if(isspace(s[i])) { c++;} } printf("\n\nTotal words are %d",c+1); return 0; } To read a String with spaces fgets (s, 40, stdin);