Monday, 9 April 2012

Position of character and How many times character repeat in string

Q. write a C program to accept a string from user and after that accept a single character from user and search whether this character include in string or not if is is include then find out the position of character and also calculate, how many times character repeat?

For example:

Entered string : This is C programming
Entered character for search: p

Position of p in string : 11
Repeation of p in string : 1

Ans.

/*c program for search character and if is include then how many times present in string*/
#include<stdio.h>
#include<conio.h>
int searchChar(char [], char);
int repeatChar(char [], char);
int main()
{
 char str[100];
 char search;
 int count,repeat,posi;
 printf("Enter any string : ");
 for(count=0; ((str[count]=getchar())!='\n'); ++count);
 str[count+1]='\0';
 printf("Enter character for search : ");
 scanf("%c", &search);
 posi = searchChar(str,search);
 repeat = repeatChar(str,search);
 if(posi == -1)
   printf("\nEntered  character is not found...");
 else
 {
   printf("\nPosition of %c in string = %d\n",search,posi+1);
   printf("\nRepeation of %c in string = %d\n",search,repeat);
 }
 getch();
 return 0;
}

/*function for search character*/
int searchChar(char str[], char ch)
{
 int i;
 for(i=0; str[i]!='\0'; i++)
 {
   if(str[i]==ch)
      return i;
 }
 return -1;
}

/*function for calculate frequency of character*/
int repeatChar(char str[], char ch)
{
 int i,rep=0;
 for(i=0; str[i]!='\0'; i++)
 {
   if(str[i]==ch)
      rep++;
 }
 return rep;
}

/**************Output**************/

Output of search character from string & find out its repetition
Screen shot for search character in string and find its frequency

Related programs:

  1. Reverse all string
  2. Reverse all words but not string
  3. Reverse only first character of string & add extra character
  4. Display string vertically
  5. Search sub string from main string
  6. Search sub string individual from main string
  7. Change title case of string

Sunday, 1 April 2012

Read and print string

Q. Write a simple C program to accept your name and print it.

Ans.

/*c program to introduction of string*/
/*c program to accept your name and print it*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char str[30];
 printf("Enter your name : ");
 gets(str);
 puts(str);
 getch();
 return 0;
}

/***************Output*******************/
Screen shot for read and print string via gets() and puts()



/*Another way to write above program to intro of string*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char str[30];
 printf("Enter your name : ");
 scanf("%s", &str);
 printf("Your name is %s",str);
 getch();
 return 0;
}


/***************Output*******************/
Screen shot for read and print string via scanf() and printf()


Related Programs:

Identify case of character

Q. Write a C program to accept a character value form user and find out, character is capital letter or small letter.

Ans.

/*c program to find out entered character is capital or small letter*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char ch;
 printf("Enter any character : ");
 scanf("%ch", &ch);
 if(ch>='A' && ch<='Z')
    printf("You entered capital letter!");
 else if(ch>='a' && ch<='z')
    printf("You entered small letter!");
 else
    printf("You entered invalid character!");

 getch();
 return 0;
}

/****************Output*****************/

Screen shot for identify entered letter is capital or small


Change case of character

Q. Write a C program to accept a character from user and convert it opposite case.

For example, if user enter character d then it is will convert in upper-case D.

Ans.

/*c program to convert character upper-case to lower case and vice verse*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char ch;
 printf("Enter any character : ");
 scanf("%ch", &ch);
 if(ch>='A' && ch<='Z')
     ch=ch+32;
 else if(ch>'a' && ch<='z')
     ch=ch-32;
 printf("Convert case of character : %c",ch);
 getch();
 return 0;
}

/*****************Output******************/

Output for change case of entered character C program
Screen shot for change case of entered character by user


Related Programs:
  1. How to identify what is entered by user
  2. How to identify case of character
  3. Read and print string

Identify what is entered

Q. Write a C program to verify that entered word is character, number or symbol.

Ans.

/*c program for identify entered word is character, number or symbol*/
#include<stdio.h>
#include<conio.h>
int main()
{
 char ch;
 printf("Enter Any word : ");
 scanf("%ch", &ch);
 if((ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
    printf("You entered Alphabet!!");
 else if(ch>='0' && ch<='9')
    printf("You entered Number!!");
 else
    printf("You entered Symbol!!");
 getch();
 return 0;
}


/********************Output*****************/
Screen shot for what is entered by user C program



Related programs:
  1. Identify case of character C program
  2. How to change case of character C program
  3. Read and print string C program