zaman360live

Basic of Strings

Aug 4th, 2021
712
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <string.h>
  3. /* UserSnippetBy ZAMAN SHEIKH v0.1
  4. Special Notes By ZaMaN ShEiKH*/
  5. int main()
  6. {
  7.     printf("\n");
  8.     system("cls");
  9.     /* Notes About: string declaration
  10.     char strngName[]={'Z','A','M','A','N','\0'}
  11.     '\0' represents the end of the string.
  12.     char strngName[]="ZAMAN";
  13.     In the above declaration NULL character
  14.     (\0) will automatically be inserted at the end of the string.
  15.     String I/O in C programming:-
  16.     string Print prinf("%s", strngName);
  17.     or puts(strngName);
  18.     StringInput: scanf("%s",&strngName);
  19.     gets(strngName); */
  20.    
  21.    /* char ZMN[30];
  22.  
  23.     //Console display using puts
  24.     puts("Enter your Nick name:");
  25.  
  26.     //Input using gets
  27.     gets(ZMN);
  28.  
  29.     printf("Length of string named ZMN: %d\n", strlen(ZMN));
  30.     printf("Size of string named ZMN: %d\n", sizeof(ZMN));
  31.     printf("LowerCase of string named ZMN: %d\n", strlwr(ZMN));
  32.     printf("UpperCase of string named ZMN: %d\n", strupr(ZMN)); */
  33.  
  34.     char ZMN1[11] = "Hey ";
  35.     char ZMN2[10] = "ZaMaN";
  36.     strcat(ZMN1,ZMN2);
  37.     printf("Output string after concatenation: %s \n", ZMN1);
  38.     printf("Size of string named ZMN1: %d\n", sizeof(ZMN1));
  39.  
  40.  
  41.     char s1[10] = "Hi ";
  42.     char s2[10] = "ZaMaN_SheiKH";
  43.     strncat(s1,s2, 6); //6 means how much number of
  44.     //string will be printf
  45.     printf("Concatenation using strncat: %s \n", s1);
  46.  
  47.  
  48.     char ss1[30] = "Amar sonar Bangla";
  49.     printf("String s1 is: %s\n", ss1);
  50.     char ss2[30] = "Ami tomay valobashi";
  51.     //this function has copied ss2 into s1
  52.     strcpy(ss1,ss2);
  53.     printf("String s1 is: %s\n", ss1);
  54.  
  55.     //Example of strncpy:
  56.     char first[30] = "string 1";
  57.     char second[30] = "string 2: I’m using strncpy now";
  58.     /* this function has copied first 10 chars of s2 into s1*/
  59.     strncpy(first,second, 12);
  60.     printf("String first is: %s\n", first);
  61.  
  62.     //Example of strcmp: Strings Compere
  63.  
  64.     char sx1[20] = "BeginnersBooL";
  65.     char sx2[20] = "BeginnersBook";//ans will be else
  66.     char sx3[20] = "BeginnersBook.com";//strncmp means judge to n number from begaining
  67.         if (strcmp(sx1, sx2) ==0)
  68.         {
  69.             printf("string 1 and string 2 are equal\n");
  70.         }
  71.         else
  72.         {  
  73.             printf("string 1 and 2 are different\n");
  74.         }
  75.         if (strncmp(sx2, sx3, 12) ==0)
  76.         {
  77.             printf("string 2 and string 3 are equal to num of 12\n");
  78.         }
  79.         else
  80.         {  
  81.             printf("string 2 and 3 are different\n");
  82.         }
  83.  
  84.         //only difference is that strcmpi() function is not case sensitive
  85.         if (strcmpi(sx1, sx2) ==0)
  86.         {
  87.             printf("string 1 and string 2 are equal\n");
  88.         }
  89.         else
  90.         {  
  91.             printf("string 1 and 2 are different\n");
  92.         }
  93.         //Quetions
  94.     /* #include <stdio.h>
  95.     #include <string.h>
  96.  
  97.     int main( )
  98.     { */
  99.         char str1[ ] = "geeks" ;
  100.         char str2[ ] = "ForGeeks" ;
  101.  
  102.         int i = strcmpi ( str1, str2 ) ;
  103.  
  104.         printf ( "The function returns = %d", i ) ;
  105.         /* return 0;
  106.     }
  107.     Output:
  108.  
  109.     The function returns = 1
  110.  
  111.     printf("hello World");
  112.  
  113.  
  114.     #include <stdio.h>
  115.     #include <string.h>
  116.     int main(void)
  117.     { */
  118.     // Compare two strings as lowercase                              */
  119.         if (0 == stricmp("hello", "HELLO"))
  120.             printf("The strings are equivalent.\n");
  121.         else
  122.             printf("The strings are not equivalent.\n");
  123.    
  124.  
  125.         if (0 == strnicmp("hello", "HELLO"))
  126.             printf("The strings are equivalent.\n");
  127.         else
  128.             printf("The strings are not equivalent.\n");
  129.     /* return 0;
  130.     }
  131.     Example that uses strnicmp()
  132.  
  133.     This example uses strnicmp to compare two strings.
  134.  
  135.     #include <stdio.h>
  136.     #include <string.h>
  137.     int main(void)
  138.     { */
  139.     char *str12 = "THIS IS THE FIRST STRING";
  140.     char *str22 = "This is the second string";
  141.     int numresult;
  142.     // Compare the first 11 characters of str12 and str22
  143.         //without regard to case */
  144.     numresult = strnicmp(str12, str22, 11);
  145.      if (numresult < 0)
  146.       printf("String 1 is less than string2.\n");
  147.      else
  148.         if (numresult > 0)
  149.          printf("String 1 is greater than string2.\n");
  150.         else
  151.              printf("The two strings are equivalent.\n");
  152.      /* return 0;
  153.     }
  154.  
  155.  
  156.  
  157.      C program to demonstrate strdup()
  158.     #include<stdio.h>
  159.     #include<string.h>
  160.  
  161.     int main()
  162.     { */
  163.     char source[] = "GeeksForGeeks";
  164.  
  165.     // A copy of source is created dynamically
  166.     // and pointer to copy is returned.
  167.     char* target = strdup(source);
  168.  
  169.     printf("%s", target);
  170.     /* return 0;
  171.     }
  172.     //Output:
  173.  
  174.     //GeeksForGeeks
  175.  
  176.  
  177.  
  178.     // C program to demonstrate strndup()
  179.     #include<stdio.h>
  180.     #include<string.h>
  181.  
  182.     int main()
  183.     { */
  184.         char source0[] = "GeeksForGeeks";
  185.  
  186.     // 5 bytes of source0 are copied to a new memory
  187.     // allocated dynamically and pointer to copied
  188.     // memory is returned.
  189.         char* target0 = strndup(source0, 5);
  190.  
  191.         printf("%s", target0);
  192.         /* return 0;
  193.     }
  194.     //Output:
  195.  
  196.     //Geeks
  197.  
  198.  
  199.     // C code to demonstrate the working of
  200.     // strrchr()
  201.  
  202.     #include <stdio.h>
  203.     #include <string.h>
  204.  
  205.     // Driver function
  206.     int main()
  207.     { */
  208.  
  209.         // initializing variables
  210.         char st[] = "GeeksforGeeks";
  211.         char ch = 'e';
  212.         char* val;
  213.  
  214.         // Use of strrchr()
  215.         // returns "ks"
  216.         val = strrchr(st, ch);
  217.  
  218.         printf("String after last %c is : %s \n", ch, val);
  219.  
  220.         char ch2 = 'm';
  221.  
  222.         // Use of strrchr()
  223.         // returns null
  224.         // test for null
  225.         val = strrchr(st, ch2);
  226.  
  227.         printf("String after last %c is : %s ", ch2, val);
  228.  
  229.        /*  return (0);
  230.     }
  231.     Output:
  232.  
  233.  
  234.     String after last e is :  eks
  235.     String after last m is :  (null)    
  236.  
  237.  
  238.  
  239.  
  240.  
  241.     // C code to demonstrate the application of
  242.     // strrchr()
  243.  
  244.     #include <stdio.h>
  245.     #include <string.h>
  246.  
  247.     // Driver function
  248.     int main()
  249.     { */
  250.  
  251.         // initializing the denomination
  252.         char denom[] = "Rs 10000000";
  253.  
  254.         // Printing original string
  255.         printf("The original string is : %s", denom);
  256.  
  257.         // initializing the initial number
  258.         char first1 = '1';
  259.         char* entire1;
  260.  
  261.         // Use of strrchr()
  262.         // returns entire1 number
  263.         entire1 = strrchr(denom, first1);
  264.  
  265.         printf("\nThe denomination value is : %s ", entire1);
  266.  
  267.         /* return (0);
  268.     }
  269.  
  270.  
  271.     //Output:
  272.  
  273.  
  274.    //he original string is : Rs 10000000
  275.     //The denomination value is : 10000000
  276.  
  277.  
  278.  
  279.  
  280.  
  281.     //strstr */
  282.         const char haystack[20] = "TutorialsPoint";
  283.         const char needle[10] = "Point";
  284.         char *ret;
  285.         ret = strstr(haystack, needle);
  286.         printf("The substring is: %s\n", ret);
  287.    
  288.     //strset
  289.         char str0[20] = "Test String";
  290.         printf("Original string is : %s", str0);
  291.         printf("Test string after strset() : %s",strset(str0,'#'));
  292.         printf("After string set: %s",str0);
  293.    
  294.     //strnset
  295.    
  296.         char str00[] = "GeeksforGeeks";
  297.         printf("Original String: %s\n", str00);
  298.         // First 5 character of string str
  299.         // replaced by character '*'
  300.         printf("Modified String: %s\n", strnset(str00, '*', 5));
  301.    
  302.         //strrev
  303.         char str[50] = "geeksforgeeks";
  304.  
  305.         printf("The given string is =%s\n",str);
  306.         printf("After reversing string is =%s",strrev(str));
  307.    
  308.         printf("\n");
  309.  
  310.  
  311.         printf("\n");
  312.     return 0;
  313. }
RAW Paste Data