Skip to content
Related Articles

Related Articles

Output of C++ programs | Set 29 (Strings)

View Discussion
Improve Article
Save Article
Like Article
  • Difficulty Level : Basic
  • Last Updated : 12 Dec, 2021

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, ", :" ); //, and ; are delimiteres.
  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. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!