Monday, June 13, 2016

C Program to check given string is Palindrome or not.


// C Program to check given string is Palindrome or not.

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

void main()
{
   char a[10], b[10];

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

   strcpy(b,a);
   strrev(b);

   if (strcmp(a,b) == 0)
      printf("Entered string is a palindrome.\n");
   else
      printf("Entered string is not a palindrome.\n");
}

Output of program

Enter your string:
ABA
Entered string is a palindrome.

C Program to count characters in given string

//Program to count characters in given string
//This program will count 'A' in given string AHMEDABAD

#include<stdio.h>
#include<string.h>
int main()
{
  char str[20]="AHMEDABAD";
  int i,cnt=0,len=0;
  
  len=strlen(str);
  
  for(i=0; i < len; i++)   {
      if( str[i] == 'A')
          cnt++;      
  } 
  printf("Total A in given string : %d",cnt);
  return 0;
}



Output:

Total A in given string : 3



* * * * *



C program to read and print your first name.

//C program to read and print your first name.


#include<stdio.h>
#include<string.h>
void main()
{
char str[10];
printf("Enter your name:");
scanf("%s",str);
printf("Your name is %s \n",str);
}
// This program will run and generate output as:
Enter your name:ABC
ABC

String Initialization

C Program To demonstrate String Initialization

#include <stdio.h> 
#include<string.h>
int main () 

char name[10];
name[0]='H';
name[1]='E';
name[2]='L';
name[3]='L';
name[4]='O';
name[5]='\0';
printf("Greeting message: %s", name); 
return 0;
}

Output of program

HELLO

Saturday, June 11, 2016

strcat function in C program.


//String Processing  - Concatenation using strcat function.

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[10]="12345", str2[10]="abcd";

    strcat(str1,str2);

    puts(str1);
return 0;
}

//Output

12345abcd

Use of strstr function in C program.


strstr in C

strstr is a function in C that is used to find the first occurrence of a substring in a string. It is declared in the <string.h> header.

Syntax:
char *strstr(const char *str1, const char *str2);
  • str1 → The main string in which you want to search.
  • str2 → The substring you are searching for.

Returns:
  • A pointer to the first occurrence of str2 in str1.
  • NULL if str2 is not found.
Example:

//String Processing using strstr function.

#include<stdio.h>
#include<string.h>
int main()
{
    char str1[10]="12345", str2[10]="23";
    int n;

    n=strstr(str1,str2);
 
    puts(n);
    return 0;
}

Output:

2345

Friday, June 10, 2016

Write an algorithm for Subtracting two Numbers.

Problem Definition: Write an algorithm for Subtracting two Numbers.

Step 1: Start.

Step 2: Read two numbers A and B.

Step 3: Answer = A - B.

Step 4: Display Answer.