COP3014 Strings

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

If the two strings are identical, then strcmp() function returns

0: Explanation: Declaration: strcmp(const char *s1, const char*s2); The strcmp return an int value that is if s1 < s2 returns a value < 0 if s1 == s2 returns 0 if s1 > s2 returns a value > 0

What will be the output of the program ? #include<stdio.h> #include<string.h> int main() { char str[] = "India\0\BIX\0"; printf("%s\n", str); return 0; }

A string is a collection of characters terminated by '\0'. Step 1: char str[] = "India\0\BIX\0"; The variable str is declared as an array of characters and initialized with value "India" Step 2: printf("%s\n", str); It prints the value of the str. The output of the program is "India".

What is the output: #include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { // -------------------------------- // strcat (destination, source) // appends source to destination. destination has to be large enough to contain the length of source including the null character. // -------------------------------- char str1[32] = "I love programming "; char str2[32] = "C on Linux"; char str3[32] = "I love programming "; char str4[32] = "C on Linux"; strcat(str1, str2); printf("After strcat, str1 = %s\n", str1); printf("Length of str1 = %ld\n", strlen(str1)); strcat(str4, str3); printf("After strcat, str1 = %s\n", str4); printf("Length of str1 = %ld\n", strlen(str4)); return 0; }

After strcat, str1 = I love programming C on Linux Length of str1 = 29 After strcat, str1 = C on LinuxI love programming Length of str1 = 29

#include <string.h> #include <stdio.h> int main(void) { // -------------------------------- // strncpy (destination, source, n) // copies n characters from source to destination. if n < strlen(destination) the rest of destination is left intact and only the first n chars get overwritten by the first n chars of source. // -------------------------------- char str1[] = "I love programming "; char str2[] = "C on Linux"; strncpy(str2, str1, 3); printf("After strncpy, str2 = %s\n", str2); }

After strncpy, str2 = I ln Linux

what is the output: #include <stdio.h> int main(void) { char string[80]; printf("Enter a string:"); gets(string); printf("The string input was: %s\n", string); return 0; }

Enter a string: IndiaBIX The string input was: IndiaBIX

#include <string.h> #include <stdio.h> int main(void) { // -------------------------------- // strtok (str, delim) // returns a substring of str from its beginning to the first delimiter // -------------------------------- char str1[] = "I lo:ve linux"; printf("%s\n", strtok(str1, ":")); return 0; }

I lo

what is the output: #include<stdio.h> int xstrlen(char *s) { int length=0; while(*s!='\0') { length++; s++; } return (length); } int main() { char d[] = "IndiaBIX"; printf("Length = %d\n", xstrlen(d)); return 0; }

Length = 8

what is the output: #include <string.h> #include <stdio.h> int main(void) { // -------------------------------- // strcpy (destination, source) // copies the source to the destination. the destination has to be large enough to contain the source along with the null terminator // -------------------------------- char str1[] = "I love programming "; char str2[] = "C on Linux"; printf("Length of str1 before strcpy = %ld\n", strlen(str1)); strcpy(str1, str2); printf("After strcpy, str1 = %s\n", str1); printf("Length of str1 after strcpy = %ld\n", strlen(str1)); return 0; }

Length of str1 before strcpy = 19 After strcpy, str1 = C on Linux Length of str1 after strcpy = 10

What is the output: #include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { // -------------------------------- // size_t strlen (string) // computes the length of the string str up to, but not including the null terminator '\0'. // -------------------------------- char str1[32] = "I love programming "; char str2[32] = "C on Linux"; printf("My first string is : %s\n", str1); printf("My second string is : %s\n", str2); printf("str1 is %ld characters long\n", strlen(str1)); printf("str2 is %ld characters long\n", strlen(str2)); return 0; }

My first string is : I love programming My second string is : C on Linux str1 is 19 characters long str2 is 10 characters long Once one of them gets executed, the others do not matter.

What is the output of the program: #include<stdio.h> int main() { char p[] = "%d\n"; p[1] = 'c'; printf(p, 65); return 0; }

Step 1: char p[] = "%d\n"; The variable p is declared as an array of characters and initialized with string "%d". Step 2: p[1] = 'c'; Here, we overwrite the second element of array p by 'c'. So array p becomes "%c". Step 3: printf(p, 65); becomes printf("%c", 65); Therefore it prints the ASCII value of 65. The output is 'A'.

what is the output: #include<stdio.h> #include<string.h> int main() { char str1[20] = "Hello", str2[20] = " World"; printf("%s\n", strcpy(str2, strcat(str1, str2))); return 0; }

Step 1: char str1[20] = "Hello", str2[20] = " World"; The variable str1 and str2 is declared as an array of characters and initialized with value "Hello" and " World" respectively. Step 2: printf("%s\n", strcpy(str2, strcat(str1, str2))); => strcat(str1, str2)) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World". => strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2. Hence it prints "Hello World".

what will be the output: #include<stdio.h> #include<string.h> int main() { printf("%d\n", strlen("123456")); return 0; }

The function strlen returns the number of characters in the given string. Therefore, strlen("123456") returns 6. Hence the output of the program is "6".

what is the output: #include <string.h> #include <stdio.h> int main(void) { char text[] = "I learn through IndiaBIX.com"; char *ptr, c = 'i'; ptr = strrchr(text, c); if (ptr) printf("The position of '%c' is: %d\n", c, ptr-text); else printf("The character was not found\n"); return 0; }

The position of 'i' is: 19

How will you print \n on the screen?

The statement printf("\\n"); prints '\n' on the screen.

What is the output: #include <stdio.h> #include <string.h> int main(void) { char *str1 = "IndiaBIX", *str2 = "ia", *ptr; ptr = strstr(str1, str2); printf("The substring is: %s\n", ptr); return 0; }

The substring is: iaBIX

Strings definition

are one-dimensional arrays of characters termianted by a null character'\0'.

Which of the following function sets first n characters of a string to a given character?

char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch

different ways to declare c-strings:

char g[8] = {'s', 't', 'r', 'i', 'n', 'g', '\0\}; char g[8] = "string"; char g[8]="str"; char g[]="string";//similar to an array

a null-terminated string contains the ______that comprise the string followed by a ____.

characters that comprise the string followed by a null.

strcmp

compares one string to another

strncat

concatenates n characters of a string to another

strcat()

concatenates two strings; concatenates str2 at the end of str1

strcpy

copy a string to another

ctrncpy

copy the first n characters of a string to another

strstr

get the first occurence of a string in another

strlen

get the length of a string

strchr

get the location of the first occurrence of a character in a string.

strrchr

get the location of the last occurrence of a character in a string

Which of the following function is more appropriate for reading in a multi-word string?

gets(); Explanation: gets(); collects a string of characters terminated by a new line from the standard input stream stdin

Which of the following function is correct that finds the length of a string?

int xstrlen(char *s) { int length=0; while(*s!='\0') { length++; s++; } return (length); }

strings are ____ terminated with the character '\0'

null terminated with the character '\0'

What will be the output: #include<stdio.h> int main() { printf(5+"Good Morning\n"); return 0; }

printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string. Hence the output is "Morning"

strtok

split the string into tokens.

#include <string.h> #include <stdio.h> int main(void) { // -------------------------------- // strchr (str, char) // searches for the 1st occurence of char in str starting from the left. it returns the string // starting at that location to the end of the string // -------------------------------- char str1[] = "I love linux"; printf("strchr = %s\n", strchr(str1,'l')); return 0; }

strchr = love linux

In C, an array of characters is called a ___.

string

what is the output: #include <stdio.h> #include <string.h> int main(void) { char *string = "abcdefghijklmnopqrstuvwxyz"; char letter = 'x'; printf("string before strnset: %s\n", string); strnset(string, letter, 13); printf("string after strnset: %s\n", string); return 0; }

string before strnset: abcdefghijklmnopqrstuvwxyz string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz

C provides a rich set of functions for ________.

string manipulation.

#include <string.h> #include <stdio.h> int main(void) { // -------------------------------- // strcasecmp (str1, str2) // compares str1 & str2 independent of case. if str1 == str2i (independent of case), the functions returns 0, otherwise it returns 1 // -------------------------------- char str1[] = "I love linux"; char str2[] = "I Love linux"; if (!strcasecmp(str1, str2)) printf("strings are the same\n"); return 0; }

strings are the same

#include <string.h> #include <stdio.h> int main(void) { // -------------------------------- // strrchr (str, char) // searches for the last occurence of char in str starting from the left (or the first occurence of the char if searching from the right).. // it returns the string starting at that location to the end of the string // -------------------------------- char str1[] = "I love linux"; printf("strrchr = %s\n", strrchr(str1, 'l')); return 0; }

strrchr = linux

The library function used to find the last occurrence of a character in a string is

strrchr() Explanation: Declaration: char *strrchr(const char *s, int c); It scans a string s in the reverse direction, looking for a specific character c.

Which of the following function is used to find the first occurrence of a given string in another string?

strstr() Explanation: The function strstr() Finds the first occurrence of a substring in another string Declaration: char *strstr(const char *s1, const char *s2); Return Value: On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1). On error (if s2 does not occur in s1), strstr returns null.


Ensembles d'études connexes

Chapter 10 - Buying, Using, Disposing

View Set

Chapter 3: Policy Provisions, Options, and Other Features

View Set

C# Ch 1, C# Ch2, Chapter 3, Chapter 7, Chapter 8, Chapter 9

View Set

Property Valuation and Financial Analysis (14%)

View Set