Reputation: 207
i need your help guys im stuck at K&R Exercise 1-13 . its about functions! I came thru almost all 1 chapter but stuck on Functions. I can't understand how to work with functions. Well I know how to do simple functions but when I came to more complicated one I'm stuck on it! Don't know how to pass the value, K&R example of power function is a bit difficult to understand. But anyway I need your help at Exercise 1 - 13 if its possible for you to complete it so I can read the code and understand how to work with functions.
Here exercise it self:
Write a program to convert its input to lower case,using a function lower(c) witch returns c if c is not a letter, and the lower case value of c if it is a letter
if you you know some links or anything witch have some useful information about how to work with more difficult functions (not like passing string to main, but arithmetic one), can you please link them.
Also this is not 2 Edition of K&R
Upvotes: 2
Views: 792
Reputation: 5836
If you have read chapter 1 of K&R , where a simple getchar()/putchar() combination with a while loop. is used to obtain and display characters , am sure you will find this program familiar.
#include<stdio.h>
int main()
{
int ch;
while( (ch = getchar()) != EOF)
{
if((ch>=65)&&(ch<=122))
{
if((ch>=97)&&(ch<=122))
ch=ch-32;
else if((ch>=65)&&(ch<=90))
ch=ch+32;
}
putchar(ch);
}
return 0;
}
Upvotes: 0
Reputation: 3538
/*
* A function that takes a charachter by value . It checks the ASCII value of the charchter
* . It manipulates the ASCII values only when the passed charachter is upper case .
* For detail of ASCII values see here -> http://www.asciitable.com/
*/
char lower(char ch){
if(ch >= 65 && ch <=90)
{ ch=ch+32;
}
return ch;
}
int main(int argc, char** argv) {
char str[50];
int i,l;
printf("Enter the string to covert ");
scanf("%s",str);
/*
Get the length of the string that the user inputs
*/
l=strlen(str);
/*
* Loop over every characters in the string . Send it to a function called
* lower . The function takes each character by value .
*/
for(i=0;i<l;i++)
str[i]=lower(str[i]);
/*
* Print the new string
*/
printf("The changes string is %s",str);
return 0;
}
Upvotes: 1