Tanti Technology

My photo
Bangalore, karnataka, India
Multi-platform UNIX systems consultant and administrator in mutualized and virtualized environments I have 4.5+ years experience in AIX system Administration field. This site will be helpful for system administrator in their day to day activities.Your comments on posts are welcome.This blog is all about IBM AIX Unix flavour. This blog will be used by System admins who will be using AIX in their work life. It can also be used for those newbies who want to get certifications in AIX Administration. This blog will be updated frequently to help the system admins and other new learners. DISCLAIMER: Please note that blog owner takes no responsibility of any kind for any type of data loss or damage by trying any of the command/method mentioned in this blog. You may use the commands/method/scripts on your own responsibility. If you find something useful, a comment would be appreciated to let other viewers also know that the solution/method work(ed) for you.

Monday 4 July 2011

C program-6

1. /* Write a C program to read a string and check whether it is *
* a palindrome or not (without using library functions). Output *
* the given string along with suitable message */

#include
#include

main()
{
char string[25], revString[25]={'\0'};
int i,length = 0, flag = 0;

clrscr();
fflush(stdin);

printf("Enter a string\n");
gets(string);

for (i=0; string[i] != '\0'; i++) /*keep going through each */
{ /*character of the string */
length++; /*till its end */
}
for (i=length-1; i >= 0 ; i--)
{
revString[length-i-1] = string[i];
}
/*Compare the input string and its reverse. If both are equal
then the input string is palindrome. Otherwise it is
not a palindrome */

for (i=0; i < length ; i++) { if (revString[i] == string[i]) flag = 1; else flag = 0; } if (flag == 1) printf ("%s is a palindrome\n", string); else printf("%s is not a palindrome\n", string); } /*End of main()*/ /*---------------------------------------------------- Output RUN 1 Enter a string madam madam is a palindrome RUN 2 Enter a string Madam Madam is not a palindrome RUN 3 Enter a string good good is not a palindrome ----------------------------------------------------------*/ ********************************************************************************* 2. /* Write a C program to read two strings and concatenate them * * (without using library functions). Output the concatenated * * string along with the given string */ #include
#include
main()
{
char string1[20], string2[20];
int i,j,pos;

strset(string1, '\0'); /*set all occurrences in two strings to NULL*/
strset(string2,'\0');

printf("Enter the first string :");
gets(string1);
fflush(stdin);
printf("Enter the second string:");
gets(string2);

printf("First string = %s\n", string1);
printf("Second string = %s\n", string2);

/*To concate the second stribg to the end of the string
travserse the first to its end and attach the second string*/
for (i=0; string1[i] != '\0'; i++)
{
; /*null statement: simply trvsering the string1*/
}

pos = i;
for (i=pos,j=0; string2[j]!='\0'; i++)
{
string1[i] = string2[j++];
}
string1[i]='\0'; /*set the last character of string1 to NULL*/

printf("Concatenated string = %s\n", string1);
}
/*---------------------------------------
Output
Enter the first string :CD-
Enter the second string:ROM
First string = CD-
Second string = ROM
Concatenated string = CD-ROM
----------------------------------------*/

*********************************************************************

3. /* Write a C program to read an English sentence and replace*
* lowercase characters by uppercase and vice-versa. Output *
* the given sentence as well as the case covrted sentence on*
* two different lines. */

#include
#include
#include

main()
{
char sentence[100];
int count, ch, i;
clrscr();
printf("Enter a sentence\n");
for(i=0; (sentence[i] = getchar())!='\n'; i++)
{
;
}
sentence[i]='\0';
count = i; /*shows the number of chars accepted in a sentence*/

printf("The given sentence is : %s",sentence);
printf("\nCase changed sentence is: ");
for(i=0; i < count; i++) { ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]); putchar(ch); } } /*End of main()*/ /*------------------------------ Output Enter a sentence Mera Bharat Mahan The given sentence is : Mera Bhaaat Mahan Case changed sentence is: mERA bHARAT mAHAN ------------------------------------------------*/ ****************************************************************** 4. /* Write a C program read a sentence and count the number of * * number of vowels and consonants in the given sentence. * * Output the results on two lines with suitable headings */ #include
main()
{
char sentence[80];
int i, vowels=0, consonants=0, special = 0;
clrscr();
printf("Enter a sentence\n");
gets(sentence);

for(i=0; sentence[i] != '\0'; i++)
{
if((sentence[i] == 'a'||sentence[i] == 'e'||sentence[i] == 'i'||
sentence[i] == 'o'||sentence[i] == 'u') ||(sentence[i] == 'A'||
sentence[i] == 'E'||sentence[i] == 'I'|| sentence[i] == 'O'||
sentence[i] == 'U'))
{
vowels = vowels + 1;
}
else
{
consonants = consonants + 1;
}
if (sentence[i] =='\t' ||sentence[i] =='\0' || sentence[i] ==' ')
{
special = special + 1;
}
}
consonants = consonants - special;
printf("No. of vowels in %s = %d\n", sentence, vowels);
printf("No. of consonants in %s = %d\n", sentence, consonants);
}
/*------------------------
Output
Enter a sentence
Good Morning
No. of vowels in Good Morning = 4
No. of consonants in Good Morning = 7
-----------------------------------------*/



***************************************************************************

5. /* Write a C program to read N names, store them in the form *
* of an array and sort them in alphabetical order. Output the*
* give names and the sorted names in two columns side by side*
* wih suitable heading */

#include
#include

main()
{
char name[10][8], Tname[10][8], temp[8];
int i, j, N;

clrscr();

printf("Enter the value of N\n");
scanf("%d", &N);

printf("Enter %d names\n", N);
for(i=0; i< N ; i++) { scanf("%s",name[i]); strcpy (Tname[i], name[i]); } for(i=0; i < N-1 ; i++) { for(j=i+1; j< N; j++) { if(strcmpi(name[i],name[j]) > 0)
{
strcpy(temp,name[i]);
strcpy(name[i],name[j]);
strcpy(name[j],temp);
}
}
}
printf("\n----------------------------------------\n");
printf("Input Names\tSorted names\n");
printf("----------------------------------------\n");
for(i=0; i< N ; i++)
{
printf("%s\t\t%s\n",Tname[i], name[i]);
}
printf("----------------------------------------\n");
} /* End of main() */
/*--------------------------------
Output
Enter the value of N
3
Enter 3 names
Monica
Laxmi
Anand

----------------------------------------
Input Names Sorted names
----------------------------------------
Monica Anand
Laxmi Laxmi
Anand Monica
----------------------------------------
---------------------------------------- */

No comments:

Post a Comment