Tuesday, July 14, 2009

C program to Insert a string into a given string,To find the specified charcter in the string?

C progarm to find the Specified character in the String


and C program to insert a string into a given string.





C program to find Fibonacci Numbers

C program to Insert a string into a given string,To find the specified charcter in the string?
1) C progarm to find the Specified character in the String


%26gt;%26gt; copy the next code to your compiler








//////////////////////////////////////... Beginig of the code


#include%26lt;stdio.h%26gt;


char toup (char c)


{


if(c%26gt;= 'a' %26amp;%26amp; c%26lt;='z')


return c - 'a' + 'A';


return c;


}


int main ()


{


char s[81], c[3];


int i,f[81],flag=0;





printf("Enter The string: ");


scanf("%80s",s);


printf("Enter the char you want to find: ");


scanf("%1s",c);


for(i=0; s[i] !='\0' ;i++)


if(toup(s[i]) == toup(c[0]))


f[i] = 1 ,flag=1;


else


f[i] = 0;


if(flag)


{


printf("------------------------------\n...


printf("\n\nThe char \"%c\" was found in :\n",c[0]);


printf("------------------------------\n...


for(i=0; s[i]!='\0' ; i++)


if(f[i] == 1)


printf("Position %i\n",i);


}


else


printf("The char \"%c\" is not found in the string.\n",c[0]);


scanf("\n\nEnter any number to Exit%i",%26amp;i);


return 0;


}


//////////////////////////////////////... End of the code


______________________________________...





2) C program to insert a string into a given string.


%26gt;%26gt; copy the next code to your compiler





//////////////////////////////////////... Beginig of the code


#include%26lt;stdio.h%26gt;





int main ()


{


char s1[101],s2[101],s[209];


int i,j,c,ind;


printf("Enter the Original String: ");


scanf("%100s",s1);


printf("Enter the subString: ");


scanf("%100s",s2);


printf("Enter the index where you want to insert the subString: ");


scanf("%i",%26amp;ind);


for(i = 0,c=0; s1[i]!='\0' ;i++,c++)


{


if(i == ind)


for(j=0; s2[j]!='\0' ;j++,c++)


s[c] = s2[j];


s[c] = s1[i];


}


s[c]='\0';


printf("\n\nThe String now is:\n");


printf("------------------\n\n%s\n\n",s)...


printf("Enter any key and then Enter to Exit: ");


scanf("%100s",s1);


return 0;


}





//////////////////////////////////////... End of the code


______________________________________...





3) C program to find Fibonacci Numbers


%26gt;%26gt; copy the next code to your compiler





//////////////////////////////////////... Begining of the code


#include%26lt;stdio.h%26gt;





int main ()


{


char s1[101];


int n,i,n1,n2,r;


printf("Enter the number of Fibonacci numbers you want: ");


scanf("%i",%26amp;n);


n1 = 1;


n2 = 1;


if(n %26lt; 1)


printf("\a\a\aThe number should be positive\n");


else


if(n == 1)


printf("1");


else


if(n %26gt; 1)


printf("1 1");


for(i=3; i%26lt;=n ; i++)


{


r = n1 + n2;


n1 = n2 ;


n2 = r;


printf(" %i",n2);


}





printf("\n\nEnter any key and then Enter to Exit: ");


scanf("%100s",s1);


return 0;


}





//////////////////////////////////////... end of the code
Reply:This is a C program to find 1st 25 numbers in the series





#include%26lt;stdio.h%26gt;


long unsigned int fib(long unsigned int,long unsigned int);


void main()


{


long unsigned int i,j=1,k=1,c;


clrscr();


printf("\n%lu\n%lu",j,k);


for(i=1;i%26lt;=23;i++)


{


c=fib(j,k);


printf("\n%lu",c);


j=k;


k=c;


}


getch();


}


long unsigned int fib(long unsigned int a,long unsigned int b)


{


long unsigned int sum;


sum=a+b;


return sum;


}





The foll is a program to scan a character in the given string


Function to be used - strchr.





Scans a string for the first occurence of a given character








#include %26lt;string.h%26gt;


#include %26lt;stdio.h%26gt;





int main(void)


{


char string[15];


char *ptr, c = 'r';





strcpy(string, "This is a string");


ptr = strchr(string, c);


if (ptr)


printf("The character %c is at position: %d\n", c, ptr-string);


else


printf("The character was not found\n");


return 0;


}
Reply:So you're asking us to give you these programs? That's a bit much...


The first request makes no sense...


The second one: This is very simple. Fibonacci numbers are 1, 1, 2, 3, 5, etc., adding the previous two terms to get the next one. So your code would involve a for() loop, at least three variables, and a printf() statement. You can probably figure it out from there...


If not, email me and I'll try to post one later.
Reply:Still sticking to C.Where are you ?.I'll suggest u to go for java.But any way i'll make a note.





C program to find specified character.I'll write the the logic.





1.Read the string into an array[]


2.Read character to replace


3.Find the length of string


4.for (int i=0;i%26lt;length;i++)


search for the specfied character.


if(char==a[i])


{


++count;


print("Occurence found at",i+1)


}





Regarding ur next question it is not clear where to insert (either at specified index or beside last character)





and last one Fibonocci series 0,0,1,1,2,3,5,8,13...





U are asked to find fibonocci number from given number





1.Read number as n


2.if(f==0)


print "This is fibonocci number"





3.set f1=0;f2=1





4. for(i=0;i%26lt;n/2;i++)


{


f3=f2+f1;


if(f3==n)


{


print "This is fibonocci number"


exit(1)


}


f1=f2;f2=f3





}





Try implement in C definetly works.


No comments:

Post a Comment