Skip to content
Related Articles

Related Articles

Improve Article
Output of C++ programs | Set 29 (Strings)
  • Difficulty Level : Basic
  • Last Updated : 18 Jun, 2020

Prerequisite: Strings

  1. Question
    What is the Output?




    #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.

  2. Question
    What is the Output?






    #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.

  3. Question
    What is the Output?




    #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.

  4. Question
    What is the Output?




    #include <iostream>
    #include <cstring>
    using namespace std;
      
    int main ()
    {
      char string[50] = "geeks, for:geeks";
      char *p;
      p = strtok (string, ", :" ); //, and ; are delimeteres.
      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 delimeters. strtok() returns the string which is before the delimeter and writes NULL immediately after the token in string.

  5. Question
    What is the Output?




    #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 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.

Want to learn from the best curated videos and practice problems, check out the C++ Foundation Course for Basic to Advanced C++ and C++ STL Course for foundation plus STL.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.



My Personal Notes arrow_drop_up
Recommended Articles
Page :