C Programming - Strings - Discussion

Discussion Forum : Strings - Point Out Correct Statements (Q.No. 1)
1.
Which of the following statements are correct about the program below?
#include<stdio.h>

int main()
{
    char str[20], *s;
    printf("Enter a string\n");
    scanf("%s", str);
    s=str;
    while(*s != '\0')
    {
        if(*s >= 97 && *s <= 122)
            *s = *s-32;
        s++;
    }
    printf("%s",str);
    return 0;
}
The code converts a string in to an integer
The code converts lower case character to upper case
The code converts upper case character to lower case
Error in code
Answer: Option
Explanation:

This program converts the given string to upper case string.

Output:

Enter a string: indiabix

INDIABIX

Discussion:
8 comments Page 1 of 1.

Nikhil said:   4 years ago
But string is always fixed and we can't change its characters right ? It should give an error.

Abhishek prajapati said:   8 years ago
Yes! We can change the value of any string with the changing each character value using pointer.

Pointer direct point the memory address where string are located.

In this question we are accessing each index of string (lover case given by user) and change the it's ASCII value corresponding it's UPPER case ASCII value using pointer.

Even in 'strcpy' function this concept already mentioned in 'string. H' header file.

Varun said:   8 years ago
Can the strings s=str copy without using strcpy? Please help me.

Dhanalakshmi said:   1 decade ago
Yes scanf("%s",str); is correct as it is string value.
But in case of integer value,

It will not show any error but when the value is made to display after getting ,it will not display the correct value instead will display the garbage value.

Eg:

printf("Enter the values:");
scanf("%d %s",a,c);
printf("The values are %d %s",a,c);

O/p:

Enter the values: 2 c
The values are -24586 c

Karthik said:   1 decade ago
Is this correct ?

scanf("%s", str);

or else,
scanf("%s", &str);

Is correct ? Help me please.

Shobana said:   1 decade ago
Lets take an e.g string: ab.

Step 1=> if(*s >= 97 && *s <= 122)
The ASCII code of a is 97,b is 98,c is 99, so now lets take first char (i.e a) which satisfies the if condition.

Step 2=> *s = *s-32;
Here, s takes value 97.so, 97-32=65 which is ASCII code of A.

Step 3=> it increments the value of s, so now second char is taken(i.e b)
So 98-32=66 which is ASCII code of B.

Now it reaches end \0.

So it comes out of while loop..

So output will be:
AB
(1)

@anonymous said:   1 decade ago
How it converts uppercase to lower case?

Varad salvi said:   1 decade ago
Perfect form 1 to 100.

Post your comments here:

Your comments will be displayed after verification.