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

Prerequisite: Strings

  1. Question
    What is the Output?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    #include <cstring>
    using namespace std;
      
    int main()
    {
        char s1[] = "geeksforgeeksforgeeks";
        char s2 = 'f';
        char *ptr = strchr( s1, s2);
        cout << ptr;
        return 0;
    }

    chevron_right

    
    

    Output:

    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.

  2. Question
    What is the Output?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    #include <cstring>
    using namespace std;
      
    int main()
    {
        char s1[] = "geeksforgeeksforgeeks";
        char s2[] = "for";
        char *ptr = strstr(s1, s2);
        cout << ptr;
        return 0;
    }

    chevron_right

    
    

    Output:

    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.

  3. Question
    What is the Output?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    using namespace std;
      
    int main()
    {
        char str[] = "geeksforgeeks";
        cout << 6[str];
        return 0;
    }

    chevron_right

    
    

    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?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #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;
    }

    chevron_right

    
    

    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?

    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    #include <iostream>
    #include <cstring>
    using namespace std;
      
    int main ()
    {
      char string[50] = "geeksforgeeks";
      memset (string, '*', 8);
      cout << string;
      return 0;
    }

    chevron_right

    
    

    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.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.