Sunday, September 7, 2014

find how many times a character appears in a string

0 comments

Problem

write a c program that will ask the user to input a character and then the program will check whether the character (how many times) exists in the following string or not.

Note: use pointers to find the character in the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
int countchar(char *p, char a);
main()
{
 int count;
 char a;
 char p[]="OMG! its hot.";
 printf("Enter the character : ");
 scanf("%c",&a);
 count = countchar(p,a);
 printf("\n\nCharacter %c occured in the array %d times.\n",a,count);
 system("pause");
}
int countchar(char *p, char a)
{
 int count=0;
 while (*p != '\0')
 {
  if (a == *p)
   count++;
  p++;
 }
 return (count);
}

Sample Output


find character in a string

Read More..

determine the number of characters in a string

0 comments

Problem

write a program in c language that passes array index as an argument to a user defined function.The function then counts the number of characters in that string and displays it to the user.

Note: use pointers to count the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
int mystrlen(char *p);
main()
{
 int count;
 char p[]="Honesty is the best policy.";
 count = mystrlen(p);
 printf("\n\nNumber of characters in the string = %d\n\n",count);
 system("pause");
}
int mystrlen(char *p)
{
 printf ("\n");
 int count = 0;
 while (*p != '\0' )
 {
  printf ("%c",*p);
  p++;
  count++;
 }
 return (count);
}

Sample Output


number of characters in a string

Read More..

print a string character by character in a user defined function where the string is declared in main

0 comments

Problem

write a program in c language that passes array index as an argument to a user defined function and print the string in that function character by character.

Note: use pointers to print the string character by character.

Solution - Code

#include<stdio.h>
#include<conio.h>
void display(char *p, char end);
main()
{
 char p[]="We are gonna be the best programmers ever.";
 char end= '\0';
 display(p,end);
 printf ("\n\n",*p);
 system("pause");
}
void display(char *p, char end)
{
 printf ("\n",*p);
 while (*p != end)
 {
  printf ("%c",*p);
  p++;
 }
}

Sample Output


display string character by character

Read More..

print a string in a user defined function, where string is declared in main function

0 comments

Problem

write a program in c language that passes array index as and argument to a user defined function and print the string in that function.

Solution - Code

#include<stdio.h>
#include<conio.h>
void display(char *p);
main()
{
 char p[]="Mr.Maaz Ali is a good boy.";
 display(p);
 system("pause");
}
void display (char *p)
{
 printf("\n%s\n\n",p);
}

Sample Output

print string in a function, defined in main

Read More..

determine string length and size

0 comments

Problem

Write a program in c language which will determine the user input string length and size.

Solution - Code

#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
 char sname[30];
 char fname[30];

 int i,a,b,c,d;

 printf("Enter student name : ");
 gets(sname);
 printf("\n");

 printf ("Enter father name : ");
 gets(fname);
 printf("\n");

 a = strlen(sname);
 b = sizeof(sname);
 printf("\nstring length & string size of %s = %d\t%d\n\n",sname,a,b);

 c = strlen(fname);
 d = sizeof(fname);
 printf("string length & string size of %s = %d\t%d\n\n",fname,c,d);
 printf("\n");

 system("pause");
}

Sample Output


length, size of string

Read More..

Copyright 2017. All Rights Reserved. Privacy Policy / Terms And Conditions / Sitemap / Contact