Advance Strings in C

code for:difference between sizeof and strlen
#include<stdio.h>
#include<string.h>
int main()
{
char str1[]="He\0\0llo\0";
printf("%s\n",str1);//He
printf("sizeof(str1)=%d\n",sizeof(str1));//8+1=9
printf("strlrn(str1)=%d\n",strlen(str1));//2

char str2[]="He\0llo";
printf("%s\n",str2);//He

char str3[]="He\0llo\0";
printf("%s\n",str3);//He

char str11[]="He\0\0llo";
printf("%s\n",str11);//He

char str22[]="He\0\llo";
printf("%s\n",str22);//He

char str33[]="He\0llo\0";
printf("%s\n",str33);//He
    return 0;
}

/*
code for:
assging element out of bound
unpridictable result

*/
#include<stdio.h>
int main()
{
    /*assiging 6 element in array of capacity of 4 element*/
char arr[4]="Hello";//actually 6 elements Hello\0
printf("%s",arr);

printf("\n");
    return 0;
}

/*
code for:
printing escape sequences and special characters
like printing "\n"
use \
*/

#include<stdio.h>
int main()
{
    printf("\\nHello \" \" India \\0 \n\n");//HelloIndia


    return 0;
}
/*

\nHello " " India \0

*/

/*
code for:
sizeof
*/
#include<stdio.h>
int main()
{
char c='A';
int i=10;
float f=3.14;

printf("%d\n",sizeof(c));//1
printf("%d\n",sizeof('c'));//4

printf("%d\n",sizeof(int));//4
printf("%d\n",sizeof(5));//4

printf("%d\n",sizeof(f));//4
printf("%d\n",sizeof(3.14));//8 double
printf("%d\n",sizeof(3.0));//8 double

    return 0;
}

/*
code for:
string and
char array
*/

#include<stdio.h>
#include<string.h>
int main()
{
    /*1. string char array terminated by \0 */
char* str1="Hello";//exactly Hello\0
char* str2="World";

char* str3;
//strcat(str1,str2);
//str3=strcat(str1,str2);
//printf("%s\n",str3);
//str1 is having memoory for Hello and we are writing beyond that so seg fault


/*2. char array */
char arr1[20]="Hello";
char arr2[20]="World";
char arr3[20];
//arr3=strcat(arr1,arr2);//assginging to read only error
printf("%s\n",strcat(arr1,arr2) );
    return 0;
}
---
/*
code for:
alignment columnwise output
*/

#include<stdio.h>
#include<string.h>

int main()
{
char ch[5];
int i;
for(i=0;i<=2;i++)
//*(ch+i)=65;
//*(ch+i)='\0';
    ch[i]=65;
printf("\n\n%s\n\n",ch);

char s1[]="Hello";
char s2[]="Who are";
char s3[]="abc def ghi";
char s4[]="dfdsfdsf fdsfdsf  ";
printf("%-20s ",s1);
printf("%-20s ",s2);
printf("%-20s ",s3);
printf("%-20s\n",s4);

printf("%-20s ",s1);
printf("%-20s ",s3);
printf("%-20s\n",s4);
printf("%-20s ",s2);

printf("%-20s ",s2);
printf("%-20s ",s3);
printf("%-20s ",s1);
printf("%-20s\n",s4);

printf("%-20s ",s1);
printf("%-20s ",s2);
printf("%-20s\n",s4);
printf("%-20s ",s3);

printf("%-20s ",s2);
printf("%-20s ",s3);
printf("%-20s\n",s4);
printf("%-20s ",s1);

printf("%-20s ",s1);
printf("%-20s ",s2);
printf("%-20s ",s3);
printf("%-20s\n\n",s4);

    return 0;
}
*
/*
   OUTPUT


AAA

Hello                Who are              abc def ghi          dfdsfdsf fdsfdsf   
Hello                abc def ghi          dfdsfdsf fdsfdsf   
Who are           Who are              abc def ghi          Hello                   dfdsfdsf fdsfdsf   
Hello                Who are              dfdsfdsf fdsfdsf   
abc def ghi       Who are              abc def ghi          dfdsfdsf fdsfdsf   
Hello                Hello                   Who are              abc def ghi          dfdsfdsf fdsfdsf   
*/
----
/*
code for static string address and values

*/
#include<stdio.h>
#include<string.h>

int main()
{
static char s[]="abc";
printf("\nstrlen(s)=%d\n",strlen(s));//3
printf("sizeof(s)=%d\n\n",sizeof(s));//4=3+1//sss\0

printf("*(s+strlen(s))=%d\n",*(s+strlen(s)));//0
/* printf("*(s+strlen(s))=%d\n",*(ibaseaddress of s + 3)));//0//3rd elt after base address which is nothing but \0 whose ascii is 0*/

printf("*(s+3)=%d\n",*(s+3));//0
printf("*(s+2)=%d\n",*(s+2));//99
printf("*(s+1)=%d\n",*(s+1));//98
printf("*(s+0)=%d\n",*(s+0));//97//
    return 0;
}
---
/*
code for:
string routines
strcpy
strcat
strchr
strstr
strlen
sizeof
*/
#include <stdio.h>
#include <string.h>

int main ()
{
    char str1[12] = "Hello";
    char str2[12] = "World";
    char str3[12];
    int  len ;

    /* copy str1 into str3 */
    strcpy(str3, str1);
    printf("strcpy( str3, str1) :  %s\n", str3 );

    /* concatenates str1 and str2 */
    strcat( str1, str2);
    printf("strcat( str1, str2):   %s\n", str1 );

    /* total lenghth of str1 after concatenation */
    len = strlen(str1);
    printf("strlen(str1) :  %d\n", len );//10

    /*sizeof string gives 1+ for null char here 12 fixed */
    printf("sizeof(str2) :  %d\n", sizeof(str2) );//12
/*strchr returns a pointer to the first occurance of char*/
   char *ptrtofirstoccurance=strchr(str1,'o');
   printf("found first occurance of o at:%s\n",ptrtofirstoccurance);
/*strstr returns a pointer to the first occurance of str*/
   char *ptrtofirstoccurance2=strstr(str1,"rl");
   printf("found first occurance of o at:%s\n",ptrtofirstoccurance2);
    return 0;
}
/*
strcpy( str3, str1) :  Hello
strcat( str1, str2):   HelloWorld
strlen(str1) :  10
sizeof(str2) :  12
found first occurance of o at:oWorld
found first occurance of o at:rld
*/

No comments:

Post a Comment