Full Transcript

UNIVERSITY INSTITUTE OF ENGINEERING DEPARTMENT- ACADEMIC UNITS FIRST YEAR ENGINEERING PROGRAMMES Subject Name : Introduction to Problem Solving Subject Code: 24CSH-101 Strings DISCOVER. LEARN. EMPOWER ...

UNIVERSITY INSTITUTE OF ENGINEERING DEPARTMENT- ACADEMIC UNITS FIRST YEAR ENGINEERING PROGRAMMES Subject Name : Introduction to Problem Solving Subject Code: 24CSH-101 Strings DISCOVER. LEARN. EMPOWER Introduction to Problem Solving Course Objectives The course aims to provide exposure to problem-solving through programming. The course aims to raise the programming skills of students via logic building capability. With knowledge of C programming language, students would be able to model real world problems. 2 Course Outcomes CO Course Outcome Numbe r CO1 Remember the concepts related to fundamentals of C language, draw flowcharts and write algorithm/pseudocode. CO2 Understand the way of execution and debug programs in C language. CO3 Apply various constructs, loops, functions to solve mathematical and scientific problem. CO4 Analyze the dynamic behavior of memory by the use of pointers. CO5 Design and develop modular programs for real world problems using control structure and selection structure. 3 ASSESSMENT PATTERN The performance of students is evaluated as follows: Theory Practical Continuous Internal Semester End Continuous Internal Semester End Components Assessment (CAE) Examination (SEE) Assessment (CAE) Examination (SEE) Marks 40 60 60 40 Total Marks 100 100 4 String Definition Initialization of string Memory Representation of Content String String I/O functions Examples References 5 Def: A string in C is simply an array of characters ended with null character (‘\0’) This null character indicates the end of the string. Strings are always enclosed by double quotes. Whereas, character is enclosed by single quotes in C. INITIALIZATION OF STRING: String char string = {‘h’, ’e’, ‘l’, ‘l’, ‘o’, ‘\0’}; (or) char string = “Hello”; (or) char string [] = “Hello”; Difference between above declarations are, when we declare char as “string”, 10 bytes of memory space is allocated for holding the string value. When we declare char as “string[]”, memory space will be allocated as per the 6 requirement during execution of the program. Let’s take an example for Memory representation of string: char string=”Hello” Memory Representat ion in If you do not place null character at the end of string, the Strings C compiler automatically places the '\0' at the end of the string when it initializes the array. If we print above mentioned #includestring in C: int main () { char string = {'H', 'e', 'l', 'l', 'o', '\0'}; printf("Message is: %s\n", string); return 0; 7 Input function scanf() can be used with %s format specifier to read a string input from the terminal. But there is one problem with scanf() function, it terminates its input on the first white space it encounters. Therefore if you try to read an input string "Hello World“ using scanf() function, it will only read Hello and terminate after encountering white spaces. String Input and Output function 8 You can use the gets() function to read a line of string. And, you can use puts() to display the string. Example1 : gets() and puts() #include How to int main() read a line { of text? char name; printf("Enter name: "); gets(name); OR fgets(name, sizeof(name), stdin); // read string printf("Name: "); puts(name); // display string return 0; } 9 Example2: Concatenate Two Strings #include int main() { char s1 = "programming ", s2[] = "is awesome"; int length, j; // store length of s1 in the length variable length = 0; while (s1[length] != '\0') Example of { ++length; string } // concatenate s2 to s1 for (j = 0; s2[j] != '\0'; ++j, ++length) { s1[length] = s2[j]; } // terminating the s1 string Here, two strings s1 and s2 and s1[length] = '\0'; concatenated and the printf("After concatenation: "); result is stored in s1. puts(s1); It's important to note that return 0; the length of s1 should be } sufficient to hold the string 1. A string in C is simply an array of characters ended with null character (‘\0’) This null character indicates the end of the string. SUMMARY 2. 3. Strings are always enclosed by double Use gets and puts quotes. Whereas, functions for input a character is enclosed string by single quotes in C. PROGRAMS Q1 Write a C program to count no of lines, words and characters in a given text. Q2 C program to print all VOWEL and CONSONANT characters separately FREQUENTLY Q3 C program to count upper case, lower case and special characters in a string. ASKED Q4 C program to print following pattern. QUESTIONS H He Hel Hell Hello 12 1 What is the Format specifier used to print a String or Character array in C Printf or Scanf function.? A) %c B) %C C) %s UTILISE D) %w YOUR 2. What is the output of C Program with Strings.? KNOWLEDGE int main() TO ANSWER { char ary[]="Discovery Channel"; Let us see how much you printf("%s",ary); have learned from the return 0; lecture and how effectively you can apply } your knowledge…!! A) D B) Discovery Channel C) Discovery D) Compiler error 13 3 What is the output of C Program with Strings.? { char str[]={'g','l','o','b','e'}; printf("%s",str); return 0; } UTILISE A) g B) globe YOUR C) globe\0 KNOWLEDGE D) None of the above 4 What is the output of C Program.? TO ANSWER int main() { int str[]={'g','l','o','b','y'}; Let us see how much you printf("A%c ",str); have learned from the lecture and how printf("A%s ",str); effectively you can apply printf("A%c ",str); your knowledge…!! return 0; } A) A A A B) A Ag Ag C) A*randomchar* Ag Ag D) Compiler error 14 Book References: Thareja Reema (2014) Programming in C. 2nd ed. Zed A. Shaw, Learn C the Hard Way’ https://en.wikibooks.org/wiki/C_Programming REFERENCES Vedio Lecture: https://spocathon.page/video/lecture-32-character-array-and-strings BOOKS https://www.youtube.com/watch?v=_3CmPbInJJs WEBSITES COURSES Websites: https://www.tutorialspoint.com/objective_c/objective_c_strings.htm https://www.programiz.com/c-programming/c-strings https://www.studytonight.com/c/c-input-output-function.php 15 THANK YOU 16

Use Quizgecko on...
Browser
Browser