Unable to convert values from lowercase to uppercase using toupper - c

i'm attempting to create a program that asks the user to firstly enter the amount of values they would like to convert from lowercase to uppercase. The for loop then assigns each value into an array. The array then goes through another for loop to convert the values into uppercase using LowerToUpper function.
When i go to run the program, it will take in values and then start doubling them on the command window, and will cease when you have completed entering the values, rather than printf the results. Could you please explain why. Thank you in advance
#include<stdio.h>
#include <string.h>
#include <ctype.h>
void LowerToUpper(char* array)
{
toupper(*array);
}
int main(void)
{
int i, amount;
printf("How many values?\n");
scanf("%d", &amount);
char *d;
char array1 [amount];
printf("Please enter the values\n");
for(i=0; i<amount; i++)
{
scanf("%c", &array1[i]);
}
for(i=0; i<amount; i++)
{
d=&array1[i];
LowerToUpper(d);
scanf("%c", &array1[i]);
printf("%c", array1[i]);
}
return 0;
}

You are not using toupper() properly. The function returns the converted value in case of success. You need to make use of the returned value. The supplied argument is not changed.
That said, the program structure is unnecessarily complicated. you can simplify it like
for(i=0; i<amount; i++)
{
int result = toupper (array1[i]);
if (result != array1[i]) printf("%c", result); //just checkin', if converted
}
That said, you have many other issue which you don't see at this moment, like
scanf("%c", &array1[i]);
this will, to your surprise, only ask you for half the number of inputs. Why? You forgot to ignore the newline entered by RETURN key.
Then, you did not check for the success of scanf("%d", &amount); call. In case, the scanning fails, you'll end up with undefined behavior.
The second scanf() inside the last for loop is probably something you don;t want, it's useless, at best.

Change this:
toupper(*array);
to this:
*array = toupper(*array);
since toupper ref's mentions:
Return Value
The uppercase equivalent to c (*array in your case), if such value exists, or c (unchanged) otherwise. The value is returned as an int value that
can be implicitly casted to char.
PS: Defining a function LowerToUpper() for this operation (one line of code) is an overkill, and you could call that one line of code inside main() instead (I mean the body of the function to be moved into main()).

Related

c-fill a one dimensional array with for loop

I can understand the first solution.But in the second solution i am confused about the way scanf, accept 4 values at the same time and apply them to the for loop.
//first solution
#include <stdio.h>
int main() {
int pin[4],i;
for(i=0; i<4; i++){
printf("Give value: ");
scanf("%d", &pin[i]);
}
return 0;
}
//second solution
#include <stdio.h>
int main() {
int pin[4],i;
printf("Give 4 values: ");
for(i=0; i<4; i++){
scanf("%d", &pin[i]);
}
return 0;
}
The difference is only that in the first example there is a printf that asks you for the input values at every iteration of the cycle while in the first example there is a printf (only one) before the cycle.
The operation that matters (the scanf) is exactly the same in the two examples.
The scanf commamd does not read four values at the same time.
This is an example of a loop: a piece of code that does the same thing multiple times.
for(i=0; i<4; i++){
scanf("%d", &pin[i]);
}
What the for statement does is, first initialize i to zero, then check that i is less than four, then, execute the code within its curly braces. That's the scanf statement, which attempts to read an integer to the address specified by &pin[i]. Since i is zero, that means, the address of pin[0], which is the first element of the array pin.
After the scanf executes (and whether or not it succeeds, which is something you may want to look into) the for statement increments the value of i, checks that it is still less than four, and executes the block of code between the braces again. This time, the scanf statement attempts to read to pin[1].
The loop executes two more times, potentially storing integers in pin[2] and pin[3] before terminating just after incrementing i to 4.

How do I determine if a value is within a range?

I am relatively new to C, I have to do it for school unfortunately and I am having issues with it at the easiest exercises.
Here I have to check if a number is in a certain interval, for example between 4 and 6. I made it like this.
#include <stdio.h>
int main(){
int i;
printf("Value to check Interval \n");
scanf("%s", i);
if (i>4 && i<6){
printf("%s Value is in first interval\n", i);
}
}
The scanf to enter the number and check if it is in the interval. But even if I enter a number that is part of it, for example 5, the printf doesn't do anything. I tried also to add an else statement for numbers outside the interval, but also there the printf did not change anything.
It is because you have declared i variable as int and you are taking input as string so when it is checking condition it is getting null value in i variable and not able to enter if block check below code
#include <stdio.h>
int main(){
int i;
printf("Value to check Interval \n");
scanf("%d",&i);
if (i>4 && i<6){
printf("%d Value is in first interval\n", i);
}
}
try compiling your code without if condition i variable will return a null value

how to scan a character and integer together in c

I am writing code for scanning character and integer together in the format like
R 2
but the following code gives unexpected result and i am not able to scan a character and integer togather
int main()
{
long int m,i;
scanf("%ld",&m);
char que[m];
long int d[m];
for (i=0; i<m; i++) {
scanf("%c ",&que[i]);
scanf("%ld",&d[i]);
}
for (i=0; i<m; i++) {
printf("%c %ld",que[i],d[i]);
}
You have to add a space before the %c in scanf() to excape the left out newline \n character.
After the change, the code works fine, check out below..
#include<stdio.h>
int main()
{
long int m,i;
scanf("%ld",&m);
char que[m];
long int d[m];
for (i=0; i<m; i++) {
scanf(" %c",&que[i]);
scanf("%ld",&d[i]);
}
for (i=0; i<m; i++) {
printf("%c %ld\n",que[i],d[i]);
}
}
First of all, if you create array like this:
char que[m];
You will create a static array of char, which is needed to be given an exact value (and also can't be changed/constant) of how big you want your array to be, so you can't put 'm' there. The same goes to:
long int d[m];
Second, if you use C, you really can't declare the data type of variable after you use a function in your program (in your case, you declare the array of char and array of long int after you put scanf()).
Third, if you press 'Enter' after give the input, the input will be read by line 1 (i = 0), and will put \n in line 2 (i = 1), because the \n is still in the buffer (in scanf() case, stdin). It's one of an error that will happen if you use scanf(). There is at least 2 solutions to this:
1.Put fflush(stdin) after every scanf() (not recommended since fflush() is supposed to be used with output stream, not input stream)
2.Create a function like this:
void clear(void) {
while (getchar() != '\n');
return;
}
and use clear() after every scanf().

Why variables set in a function are lost?

I am trying to pass the parameters in c through a function. The version with a string argument is working fine but both versions with integer arguments are returning 1 as a result.
#include<stdio.h>
void main()
{
char s1[10];
int a,b;
clrscr();
printf("name=%s\n",getname(s1));
printf("mobile=%d\n",getmobile(a));
printf("mobile=%d\n",getrno(b));
getch();
}
getname(char s[10])
{
printf("enter the name\n");
gets(s);
return ;
}
getmobile(int a)
{
printf("enter the mobile number\n");
scanf("%d",&a);
}
getrno(int b)
{
printf("enter the rno\n");
scanf("%d",&b);
}
The reason why your getname works is but getrno doesn't is because of pass-by-reference vs. pass-by-value semantics and because arrays, like s1 decay to pointers. These are important concepts to understand if you want to program in C.
Think of it like this: When you call getname it accepts a local copy of the address of a buffer. The function then writes into the buffer itself. But when you call your getrno the function accepts a local copy of an integer and reads the value into that local copy, so that nothing changes in the program outside.
#askmish has proposed a good solution, but I would strongly advise something like this instead:
// getrno will prompt the user to enter the rno and will store it into the variable
// pointed to by b. If the function returns 1 then a value was successfully read.
int getrno(int* b)
{
// make sure that the pointer looks valid
if (b == NULL)
return 1;
// prompt the user to enter the text
puts ("enter the rno: ");
// Note the use of a single space at the beginning of the format string
// which is used to consume any whitespace (including return characters
// that might be present)
if (scanf (" %d", b) == 1)
return 0;
// We couldn't read one integer. Return an error.
return 1;
}
int main()
{
int x;
if (!getrno (&x))
printf ("rno = %d\n", x);
else
printf ("failed to get rno!");
return 0;
}
You ask how to go about doing floating-point numbers. The solution is to write a function which accepts, as necessary, either a float or a double pointer and which then calls scanf with the correct format specifier, to read the value into that pointer. This function would look very much like the getrno I showed you above.
Functions should be written like this, for example:
int getrno(int b)
{
printf("enter the rno\n");
scanf("%d",&b);
return b;
}
to get a return value aka, it must have a return-type and a return statement returning value of the specified return-type. Your code had both missing.
I would also suggest to read a good book in C or atleast this page in wikibooks, to understand better and writing better code.

How to limit the input on a 2D array so it won't blow up?

I have a small program, where i say the number of lines and columns of a array I want to input, then input info to fill that array with data. What it does next it's not important so ill just omit that part of the code and put (...) in it.
int main (){
int nl, nc,i,j,z,n;
scanf ("%d %d\n", &nl,&nc);
char matrix [nl] [nc];
for (i=0;i<nl;i++)
for (j=0;j<nc;j++)
scanf(" %c",&matrix[i][j]);
scanf("%d",&n);
int s[n*2];
for (z=0;z<n*2;z++)
scanf("%d",&s[z]);
int y=0;
char s2[n];
for (z=0;z<n*2;z+=2){
s2[y]=matrix [(s[z])-1][(s[z+1])-1];
y++;
}
for (z=0;z<n;z++)
printf ("%c", s2[z]);
return 0;
}
My problem is, that it this blows up if input more chars than I should. For example if my input is:
2 3
ABC
DEF
This works just fine.
But if I put:
2 3
ABC
DEFF
It give me a segmentation fold and stops the program. Keep in mind that I have a space before the "%c" in scanf so it's ignoring the "\n" and spaces I put in the input.
What can I do to stop that extra chars in the array from blowing up?
scanf("%d",&n);
int s[n*2];
This code tries to scan and convert whatever is left in the input after reading the matrix. If the input is not numeric, as will be the case if you enter more letters than the matrix should contain, the conversion will fail and n will remain uninitialized. Then int s[n*2]; is undefined because n is indeterminate.
If you want to ignore some characters in the input, you need to do so explicitly. You also better check return values of all functions that take user input, and verify that the values read are sensible.
Ok i figured out that the problem with it was input going to the buffer. To solve this i cleared the buffer before the next input using:
while (getchar() != '\n');
Your problem is filling the array over that size.
You get your input character by character and if you enter character more than your array size the program will stopped or has been logical error,
So you can use getche() and check the array constraint.
You can edit your code as follow:
int main (){
int nl, nc,i,j;
scanf ("%d %d\n", &nl,&nc);
char matrix [nl] [nc];
for (i=0;i<nl;i++)
for (j=0;j<nc;j++)
matrix[i][j]=getche();
(...)
return 0;
}
Use %s instead of %c and remove the inner loop. So the code will be something like this:
for(i=0; i<nl; i++)
{
scanf("%s", &matrix[i]);
}

Resources