Tampilkan postingan dengan label Programming. Tampilkan semua postingan
Tampilkan postingan dengan label Programming. Tampilkan semua postingan

Program To Find The Number Of Lines In A Text File/Programming Tricks

Posted by Unknown Selasa, 21 Januari 2014 0 komentar
Here sharing a program to find no.of lines in a text file, as we know Number of lines in a file can be determined by counting the number of new line characters present.

#include
int main()
/* Ask for a filename and count number of lines in the file*/
{
//a pointer to a FILE structure
FILE *fp;
int no_lines = 0;
// consider 40 character string to store filename
char filename[4 0], sample_chr ;
//asks user for file name
printf("En ter file name:");
// receives file name from user and stores in a string named'filename'
scanf("%s" , filename);
//open file in read mode
fp = fopen(file name,"r");
//get character from file and store in sample_chr
sample_chr = getc(fp);
while (sample_ch r != EOF){
// Count whenever sample_chr is'\n'(new line) is encountere d
if (sample_ch r =='\n')
{
// increment variable'no_lines' by 1
no_lines=n o_lines+1;
}
//take next character from file.
sample_chr = getc(fp);
}
fclose(fp) ; //close file.
printf("Th ere are %d lines in %s \n", no_lines, filename);
return 0;
}

Output:
Enter file name:abc.t xt
There are 4 lines in abc.txt
 
NOTE:
In this program, name of the file to be read is taken as input. A file by the given name is opened in read-modeusing a File pointer 'fp'. Characters from the file are read into a char variable' sample_chr with the help of getc function. If a new line character( '\n') is encountered , the integer variable no_lines' is incremented. If the character read into sample_char is not a new line character, next character is read from the file. This process is continued until the last character of the file(EOF) is encountered. The file pointer is then closed and the total number of lines is shown as output

Baca Selengkapnya ....

Program to compare two strings without using strcmp() function.

Posted by Unknown Jumat, 10 Januari 2014 0 komentar
Friends I'm sharing a program where you can compare two strings without using strcmp() function. we all know that strcmp() function compares two strings. strcmp is declared in stdio.h

Case 1: when the strings are equal, it returns zero.


Case 2: when the strings are unequal, it returns the difference between ASCII values of the characters that differ.


a) When string1 is greater than string2, it returns positive value.


b) When string1 is lesser than string2, it returns negative value.


Syntax:
int strcmp (const char *s1, const char *s2);



#include
#include
int cmpstr(cha r s1[10], char s2[10]);
int main(){
char arr1[10] ="Nodalo";
char arr2[10] ="nodalo";
printf("%d", cmpstr(arr 1, arr2));
// cmpstr() is equivalent of strcmp()
return 0;
}/
/s1, s2 are strings to be compared
int cmpstr(cha r s1[10], char s2[10]){
//strlen function returns the length of argument string passed
int i = strlen(s1) ;
int k = strlen(s2) ;
int bigger;
if (i<k){
bigger = k;
}
else if (i>k){
bigger = i;
}
else{
bigger = i;
}
//loops'bigger'times
for (i = 0; i<bigger; i++){
// if ascii values of characters s1[i], s2[i] are equal do nothing
if (s1[i] == s2[i]){
}
//else return the ascii difference
else{
return (s1[i] - s2[i]);
}
}
//return 0 when both strings are same
//This statement is executed only when both strings are equal
return (0);
}
 
Output:
-32

NOTE:
cmpstr() is a function that illustrate s C standard function strcmp(). Strings to be compared are sent as arguments to cmpstr().


Each character in string1 is compared to its corresponding character in string2. Once the loop encounters a
differing character in the strings, it would return the ASCII difference of the differing characters and exit.

Baca Selengkapnya ....

Program to print a semicolon without using semicolon in the code

Posted by Unknown Sabtu, 04 Januari 2014 0 komentar
Hello friends,


Today I'm gonna share a program where you can  print a semicolon  as a output without using semicolon in the code.When we use printf("") statement we have to use semicolon at the end.If we want to print a semicolon, we use the statement: printf(";" );In above statement, we are using two semicolons . The task of printing a semicolon without using semicolon anywhere in the code can be accomplished by using the ASCII value of';'which is equal to 59.

#include
int main(void)
 {
//prints the character with ascii value 59, i.e., semicolon
if (printf("% c\n", 59))

{
//prints semicolon
}
return 0;
}
Output:
;

NOTE:

If statement checks whether return value of printf function is greater than zero or not. The return value of function
call printf("%c ",59) is 1. As printf returns the length of the string printed. printf("%c ",59) prints ASCII value that
correspond s to 59, that is semicolon( .

Baca Selengkapnya ....

Program to check whether the given string is a Palindrome

Posted by Unknown Sabtu, 31 Agustus 2013 0 komentar
Hello friends..

Writing a program is a common thing if you are a CS students and in the beginning its very difficult some times  to write the programs .. Hence  we thought to share some basic programs  which is quit useful for you all while writing assignments or practicals.

 Here sharing  a program to check whether the given string is a Palindrome .  If you are thinking what is it then  Palindrome is a string, which when read in both forward and backward way is same.
Example: radar, madam, pop, lol, Malayalam


#include
#include
int main(){
char string1[20 ];
int i, length;
int flag = 0;
printf("Enter a string: \n");
scanf("%s" , string1);
length = strlen(str ing1);
for(i=0;i<length ;i++){
if(string1 [i] != string1[le ngth-i-1]) {
flag = 1;
break;
}
}
if (flag){
printf("%s is not a palindrome \n", string1);
}
else{
printf("%s is a palindrome \n", string1);
}
return 0;
}
Output:
Enter a string: radar
"radar"is a palindrome

Explanation with example for making it easy to understand.


To check if a string is a palindrome or not, a string needs to be compared with the reverse of itself.
Consider a palindrome string:"radar",
---------- ---------- -------
index: 0 1 2 3 4
value: r a d a r
---------- ---------- -------

To compare it with the reverse of itself, the following logic is used:
0th character in the char array, string1 is same as 4th character in the same string.
1st character is same as 3rd character.
2nd character is same as 2nd character.

i'th character is same as 'length-i- 1'th character.
If any one of the above condition fails, flag is set to true(1), which implies that the string is not a palindrome .
By default, the value of flag is false(0). Hence, if all the conditions are satisfied, the string is a palindrome .



Baca Selengkapnya ....
Trik SEO Terbaru support Online Shop Baju Wanita - Original design by Bamz | Copyright of android jones.