Sunday, 6 April 2014

Strings

passing 2D String through pointers
#include<stdio.h>
void addrec(char (*p)[2][20],int);
main()
{
char arr[5][2][20];
int i,j;
printf("enter string of 2D_Array\n");
for(i=0;i<5;i++)
for(j=0;j<2;j++)
scanf(" %[^\n]s",arr[i][j]);
addrec(arr,5);
}
void addrec(char (*p)[2][20],int n)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<2;j++)
printf("%s\t",p[i][j]);
printf("\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter string of 2D_Array
well
come
madan
reddy
hyd    
andhra
india
500082
hello
gud morning
well come 
madan reddy 
hyd andhra 
india 500082 
hello gud morning
program to view differences between length of string and size of string
#include<stdio.h>
main()
{
int size1,size2,size3,len1,len2,len3;
char str1[]={'V','E','0','T','O','R','\0'};
char str2[]={'X','Y','Z','\0'};
char str3[]="vector";
printf("str2=%s\n",str2);
size1=sizeof(str1);
size2=sizeof(str2);
size3=sizeof(str3);
printf("size1=%d size2=%d size3=%d\n",size1,size2,size3);
for(len1=0;str1[len1];len1++)
;
printf("length1=%d\n",len1);
for(len2=0;str2[len2];len2++)
;
printf("length2=%d\n",len2);
for(len3=0;str3[len3];len3++)
;
printf("length3=%d\n",len3);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
str2=XYZ
size1=7 size2=4 size3=7
length1=6
length2=3
length3=6

program to remove a particular character from string
#include<stdio.h>
#include<string.h>
main()
{
char str1[50],*ptr,ch;
int i,j,count=0;
printf("enter string str1\n");
scanf("%[^\n]s",str1);
printf("enter any character\n");
scanf(" %c",&ch);
/*for(i=0;str1[i];i++)
{
if(str1[i]==ch)
strcpy(str1+i,str1);
i--;
}*/
ptr=&str1[0];
while(ptr=strchr(ptr,ch))
{
strcpy(ptr,ptr+1);
//ptr++;
}
printf("%s\n",str1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array3.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter string str1
welcome
enter any character
c
welome
Program to remove a particular string from existing string part
#include<stdio.h>
#include<string.h>
main()
{
char ch,str1[50],str2[50],*ptr;
printf("enter a string\n");
scanf("%[^\n]s",str1);
printf("enter string2\n");
scanf(" %[^\n]s",str2);
/*for(i=0;str[i];i++)
{
if(str[i]==ch)
strcpy(str+i,str+i+1);
i--;
}*/
ptr=str1;
while(ptr=strstr(ptr,str2))
{
strcpy(ptr,ptr+strlen(str2));
//memset(ptr,'m',strlen(str2));
}
printf("string=%s\n",str1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array4.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome
enter string2
com
string=wele
Program to print a string
#include<stdio.h>
#include<string.h>
main()
{
char str[50];
int i;
printf("enter a string\n");
scanf("%[^\n]s",str);
for(i=0;str[i];i++)
{
if(!((str[i]>='A')&&(str[i]<='Z')||(str[i]>='a')&&(str[i]<='z')))
{
strcpy(str+i,str+i+1);
i--;
}
}
printf("string is %s\n",str);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
we34com344 madan
string is wecommadan
Program to find character location in a string
#include<stdio.h>
#include<string.h>
main()
{
char str[50],ch,*ptr;
int i;
printf("enter a string\n");
scanf("%[^\n]s",str);
//printf("%s",str);
printf("enter a character\n");
scanf(" %c",&ch);
//ptr=strchr(str,ch);
printf("strng address is at location:%u\n",str[0]);
//printf("ptr=%u\n",ptr);
for(i=0;str[i];i++)
{
if(str[i]==ch)
printf("char found at %d location\n",i);
}

/*if(ptr==NULL)
printf("character not found");
else
printf("found at %u\n",ptr-str);
*/
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array7.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome vector institute
enter a character
v
strng address is at location:119
char found at 8 location
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome
enter a character
e
strng address is at location:119
char found at 1 location
char found at 6 location
program to find how many times str2 found in str1
#include<stdio.h>
#include<string.h>
main()
{
char str1[100],str2[50],*ptr;
char ch;
int k=0;
printf("enter a string1\n");
scanf("%[^\n]s",str1);
printf("enter a string2\n");
scanf(" %[^\n]s",str2);
ptr=str1;
while(ptr=strstr(ptr,str2))
{
k++;
ptr++;
}
printf("number of times string is repeated=%d\n",k);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array8.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string1
welcome welcome come coming cometo home
enter a string2
come
number of times string is repeated=4
Program to reverse given string
#include<stdio.h>
#include<string.h>
main()
{
char str[50],ch,*ptr1,*ptr;
int i,j;
printf("enter a string\n");
scanf("%[^\n]s",str);
for(i=0,j=strlen(str)-1;i<j;i++,j--)
{
ch=str[i];
str[i]=str[j];
str[j]=ch;
}
printf("%s",str);
printf("\n");
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array9.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome madan reddy hyderabad 
dabaredyh ydder nadam emoclew
Program to replace particular character with * symbol
#include<stdio.h>
#include<string.h>
main()
{
char str1[50],str2[50],ch,*ptr;
int i,j,index;
printf("enter a string1\n");
scanf("%[^\n]s",str1);
//intf("enter a string2\n");
//anf(" %[^\n]s",str2);
//strcat(str1,str2);
for(i=0,j=strlen(str1)-1;i<j;i++,j--)
{
ch=str1[1];
str1[1]=str1[4]=34;
str1[4]=ch;
}
printf("ptr=%s",str1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string1
welcome
ptr=w"lc"me
replace str2 in str1 at particular index value
#include<stdio.h>
#include<string.h>
char* mystrcat(char*,char*,int);
//int mystrcmp(char*,char*);
main()
{
char str1[50],str2[50];
int n;
printf("enter string1\n");
scanf("%[^\n]s",str1);
printf("enter string2\n");
scanf(" %[^\n]s",str2);
printf("enter index value\n");
scanf("%d",&n);
mystrcat(str1,str2,n);
printf("str1=%s str2=%s\n",str1,str2);
}
char* mystrcat(char* s1,char* s2,int n)
{
memmove(s1+n-1+strlen(s2),s1+n-1,strlen(s1+n-1));
memcpy(s1+strlen(s2)-1,s2,strlen(s2));
//strcat(s1+3,s2);
//printf("s1=%s s2=%s\n",s1,s2);
return(s1,s2);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array11.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter string1
welcome
enter string2
com
enter index value
2
str1=wecomlcome@ str2=com
Program to reverse string
#include<stdio.h>
#include<string.h>
char* myrev(char *);
main()
{
char str[50];
printf("enter a string\n");
scanf("%[^\n]s",str);
//n=strlen(str);
myrev(str);
printf("%s",str);
}
char* myrev(char* s1)
{
int n,i,j;
char temp;
n=strlen(s1);
for(i=0,j=n-1;i<j;i++,j--)
{
temp=s1[i];
s1[i]=s1[j];
s1[j]=temp;
}
return(s1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array12.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome
emoclew
program to remove str2 from str1
#include<stdio.h>
#include<string.h>
char* mystrcmp(char*,char*);
main()
{
char str1[50],str2[50],ch;
printf("enter string1\n");
scanf("%[^\n]s",str1);
//printf("enter string2\n");
//scanf(" %[^\n]s",str2);
printf("enter a string2\n");
scanf(" %s",str2);
mystrcmp(str1,str2);
printf("str1=%s str2=%s\n",str1,str2);
}
char* mystrcmp(char* s1,char* s2)
{
char *p;
int count=0;
p=s1;
while(p=strstr(p,s2))
{
count++;
//strcat(p," ");
//p++;
strcpy(p,p+(strlen(s2)));
//memset(p,' ',strlen(s2));
//p++;
}
printf("%d\n",count);
return(s1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array13.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter string1
welcome
enter a string2
com
1
str1=wele str2=com
program to remove a particular str2 from str1
#include<stdio.h>
#include<string.h>
char* mystrcmp(char*,char*);
main()
{
char str1[50],str2[50],ch;
printf("enter string1\n");
scanf("%[^\n]s",str1);
//printf("enter string2\n");
//scanf(" %[^\n]s",str2);
printf("enter string2\n");
scanf(" %[^\n]s",str2);
mystrcmp(str1,str2);
printf("str1=%s str2=%s\n",str1,str2);
}
char* mystrcmp(char* s1,char* s2)
{
char *p;
int count=0;
p=s1;
while(p=strstr(p,s2))
{
count++;
//strcat(p," ");
//p++;
strcpy(p,p+(strlen(s2)));
//memset(p,' ',strlen(s2));
//p++;
}
printf("%d\n",count);
return(s1);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array14.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter string1
welcome
enter string2
com
1
str1=wele str2=com
program to print upper case letters of given string
#include<stdio.h>
main()
{
char str[100],n,i;
printf("enter a string\n");
scanf("%[^\n]s",str);
printf("entered string is %s\n",str);
for(i=0;str[i];i++)
{
if((str[i]>='A')&&(str[i]<='Z'))
{
str[i]+=32;
printf("lower case is %c\n",str[i]);
}
else if((str[i]>='a')&&(str[i]<='z'))
{
str[i]-=32;
printf("upper case alphabet is %c\n",str[i]);
}
else
printf("entered character is not an alphabet\n");
}
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array16.c
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome
entered string is welcome
upper case alphabet is W
upper case alphabet is E
upper case alphabet is L
upper case alphabet is C
upper case alphabet is O
upper case alphabet is M
upper case alphabet is E
Program to print sum of integers through command line arguments
#include<stdio.h>
#include<stdlib.h>
main(int argc,char **argv)
{
int *a,i,sum=0;
a=(int *)malloc(argc*sizeof(int *));
for(i=1;i<argc;i++)
a[i]=malloc(argc*sizeof(int));
for(i=1;i<argc;i++)
{
a[i]=atoi(argv[i]);
sum=sum+a[i];
}
printf("sum=%d\n",sum);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array18.c
array18.c: In function ‘main’:
array18.c:8:5: warning: assignment makes integer from pointer without a cast [enabled by default]
 a[i]=malloc(argc*sizeof(int));
     ^
madan@madan-Lenovo-G570:~/madan$ ./a.out 10 20
sum=30
madan@madan-Lenovo-G570:~/madan$ ./a.out 10 20 40 50 60
sum=180
gets function ussage to read string from keyboard
#include<stdio.h>
#include<string.h>
main()
{
//char str1[]={'a','b','c','d'};
char str1[100];
char str2[100];
printf("enter a string\n");
gets(str1);
//str1[strlen(str1)-1]='\0';
printf("str1=%s\t str2=%s\n",str1,str2);
strcpy(str2,str1);
str2[9]='\0';
printf("str1=%s\t str2=%s\n",str1,str2);
}
OutPut:
madan@madan-Lenovo-G570:~/madan$ cc array19.c
array19.c: In function ‘main’:
array19.c:9:1: warning: ‘gets’ is deprecated (declared at /usr/include/stdio.h:638) [-Wdeprecated-declarations]
 gets(str1);
 ^
/tmp/ccm6ljlv.o: In function `main':
array19.c:(.text+0x2f): warning: the `gets' function is dangerous and should not be used.
madan@madan-Lenovo-G570:~/madan$ ./a.out
enter a string
welcome
str1=welcome  str2=  @-�
str1=welcome  str2=welcome