Output of C++ programs | Set 29 (Strings)
Prerequisite: Strings
- Question
What is the Output?
CPP
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[] = "geeksforgeeksforgeeks";
char s2 = 'f' ;
char *ptr = strchr ( s1, s2);
cout << ptr;
return 0;
}
|
Output:
forgeeksforgeeks
- Description: strchr( str, c) returns a pointer to the first occurrence 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?
CPP
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
char s1[] = "geeksforgeeksforgeeks";
char s2[] = " for ";
char *ptr = strstr (s1, s2);
cout << ptr;
return 0;
}
|
Output:
forgeeksforgeeks
- Description: strstr( str1, str2) returns a pointer to the first occurrence 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?
CPP
#include <iostream>
using namespace std;
int main()
{
char str[] = "geeksforgeeks";
cout << 6[str];
return 0;
}
|
Output:
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?
CPP
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char string[50] = "geeks, for :geeks";
char *p;
p = strtok (string, ", :" );
while (p != NULL)
{
cout << p << endl;
p = strtok (NULL, ", :");
}
return 0;
}
|
Output:
geeks
for
geeks
- Description: strtok() is used to tokenize or phrase the string using delimiters. strtok() returns the string which is before the delimiter and writes NULL immediately after the token in string.
- Question
What is the Output?
CPP
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char string[50] = "geeksforgeeks";
memset (string, '*' , 8);
cout << string;
return 0;
}
|
Output:
********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.
Last Updated :
15 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...