Output of C++ programs | Set 29 (Strings)
Prerequisite: Strings
-
Question
What is the Output?#include <iostream>#include <cstring>usingnamespacestd;intmain(){chars1[] ="geeksforgeeksforgeeks";chars2 ='f';char*ptr =strchr( s1, s2);cout << ptr;return0;}chevron_rightfilter_noneOutput:
forgeeksforgeeks
Description: strchr( str, c) returns a pointer to the first occurance of character ‘c’ in str. Here s2 is ‘f’, strchr() returns the address where it occurs for the first time in s1.
-
Question
What is the Output?#include <iostream>#include <cstring>usingnamespacestd;intmain(){chars1[] ="geeksforgeeksforgeeks";chars2[] ="for";char*ptr =strstr(s1, s2);cout << ptr;return0;}chevron_rightfilter_noneOutput:
forgeeksforgeeks
Description: strstr( str1, str2) returns a pointer to the first occurance of string str2 in str1. Here s2 is “for”, strstr() returns the address where it occurs for the first time in s1.
-
Question
What is the Output?#include <iostream>usingnamespacestd;intmain(){charstr[] ="geeksforgeeks";cout << 6[str];return0;}chevron_rightfilter_noneOutput:
o
Description: For the compiler 6[str] is same as str[6]. so, it will search for the 6th element in the string “str” and print it which is ‘o’ in this case.
-
Question
What is the Output?#include <iostream>#include <cstring>usingnamespacestd;intmain (){charstring[50] ="geeks, for:geeks";char*p;p =strtok(string,", :");//, and ; are delimeteres.while(p != NULL){cout << p << endl;p =strtok(NULL,", :");}return0;}chevron_rightfilter_noneOutput:
geeks for geeks
Description: strtok() is used to tokenize or phrase the string using delimeters. strtok() returns the string which is before the delimeter and writes NULL immediately after the token in string.
-
Question
What is the Output?#include <iostream>#include <cstring>usingnamespacestd;intmain (){charstring[50] ="geeksforgeeks";memset(string,'*', 8);cout << string;return0;}chevron_rightfilter_noneOutput:
********geeks
Description: memset( string, c, n) sets first n characters of string to ‘c’. In this program first ‘8’ characters in the string will be set to ‘*’. We often see this kind of text on the checks, where we want to hide some data. For more details on memset Refer here
This article is contributed by I.HARISH KUMAR. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Output of Python Programs | Set 19 (Strings)
- Output of Java Programs | Set 52 (Strings Class)
- Output of C programs | Set 48
- Output of C++ programs | Set 44
- Output of C programs | Set 46
- Output of C programs | Set 45
- Output of C programs | Set 54
- Output of C++ programs | Set 42
- Output of C++ programs | Set 22
- Output of C programs | Set 32
- Output of C programs | Set 34
- Output of C programs | Set 51
- Output of C++ programs | Set 35
- Output of C++ programs | Set 45
- Output of C programs | Set 52



