Changing The Case of A Letter in C? - c

I'm trying to change the case of a letter entered by the user and store a lower case and a higher case version of the letter in variables. I've written the code below but it's having issues running. Anyone point out what's causing the problems?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
char CaseChange(character){
int lowerc, higherc;
if(isupper(character)){
lowerc = tolower(character);
printf("%s", lowerc);
}
else{
higherc = character;
printf("%s", higherc);
}
return;
}
int main(void){
char character;
printf("Enter a character: ");
scanf("%c", character);
CaseChange(character);
return 0;
}

There are two problems in your code:
printf("%s", ...) is meant for outputting strings (char* and const char*), not single characters. Use printf("%c", ...)
You forgot to #include <ctype.h>
Side-note: You don't have to check if a character is uppercase with isupper(x). tolower(x) will leave already-lowercase characters intact.

Related

While calculating length of the string array, any string characters entered after space are ignored

a beginner here. I was trying to get the length of entered string without using strlen() function. I wrote a program that counts each character present in the entered string until it reaches the null terminator (\0).
After running the program, I was able to calculate the length of the first word, but not the entire sentence.
Example : When I entered "Hello how are you?" , it calculated the length of the string only until "hello", the other characters after space were ignored. I want it to calculate the length of entire sentence.
Here's my code below.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
int main()
{
char str1[100];
int i = 0;
int count = 0;
printf("Enter the string you want to calculate (size less than %d)\n", 100);
scanf("%s", str1);
while (str1[i] != '\0') //count until it reaches null terminator
{
++i;
++count;
}
printf("The length of the entered string is %d\n", count);
return 0;
}
The %s format specifier reads characters up until the first whitespace character. So you'll only read in one word a a time.
Instead, use fgets, which reads a whole line of text at a time (including the newline).
Modify the code like this, which will solve the problem.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
int main()
{
char str1[100];
int i = 0;
int count = 0;
printf("Enter the string you want to calculate (size less than %d)\n", 100);
scanf("%[^\n]s", str1);
while (str1[i] != '\0') //count until it reaches null terminator
{
++i;
++count;
}
printf("The length of the entered string is %d\n", count);
return 0;
}

Why is the value of my strcmp not equal to 0 or invalid?

I am still new to programming and there are a lot of things I still don't know but I'd like to ask why my if statement doesn't seem to be working properly. It seems the value
of strcmp(bookName, tolower(searchedName)) when the variable searchedName = "introduction to c" is not 0.
Why is this?
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char bookName[30] = "introduction to c programming";
char searchedName[30];
printf("Enter the book you are searching for: ");
scanf("%s", &searchedName);
if (strcmp(bookName, tolower(searchedName)) != 0) {
printf("The book is not in elibrary");
} else {
printf("The book is in elibrary");
}
return 0;
}
tolower() is for converting characters, not strings. You will have to apply it to each characters in the string separately.
You don't need & before arrays in this case because arrays in expressions are automatically converted to pointers (except for some case).
%s in scanf() will stop at whitespace character. %[^\n] is useful to read until hitting a newline character.
Try this:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main() {
char bookName[30] = "introduction to c programming";
char searchedName[30];
char searchedName_lower[30];
int i;
printf("Enter the book you are searching for: ");
scanf("%[^\n]", searchedName);
i = 0;
do {
searchedName_lower[i] = tolower((unsigned char)searchedName[i]);
} while (searchedName[i++] != '\0');
if (strcmp(bookName, searchedName_lower) != 0) {
printf("The book is not in elibrary");
} else {
printf("The book is in elibrary");
}
return 0;
}

Checking the return value of `isalpha`

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <string.h>
int main() {
char userPassword[20];
printf("Type in your password: \n");
scanf("%c", &userPassword);
if (isalpha(userPassword) == 0) {
printf("Nice");
} else {
printf("Nope");
}
return 0;
}
I'm trying to come up with a code where to check if the password contains only letters. Why does this code only work with the " == 0" sign. My friend told me to put this and my code works. What does the " == 0" do?
The signature of isalpha is int isalpha ( int c ).
Parameters
c character to classify
Return value
Non-zero value if the character is an alphabetic character, zero otherwise.
So, if c is not alpha, it returns non-zero, otherwise 0.
About the program:
scanf needs char *, not &userPassword, which is char **. scanf("%s", userPassword) is OK.
pass char to isalpha instead of char *.
If you want to check if a string is all alpha, you can simply iterate the string and check each single character. Like:
bool is_all_alpha(char *s) {
for (; *s!='\0'; ++s) {
if (!isalpha(*s)) return false;
}
return true;
}
http://en.cppreference.com/w/cpp/string/byte/isalpha

Setting last character in a string to 0 causes program crash

This is for a homework assignment, so it can not use loops of any kind as a way to force recursion practice. I am also not to change the method signature, or anything in the main() function.
The function is intended to use recursion to print a string in reverse. I learned on this site (Strip first and last character from C string) how to remove the last character in a string. When I try and reproduce it in my code, the program crashes on execution. Here is that code:
#include <stdio.h>
#include <string.h>
void print_reverse_str(char *str) {
if (strlen(str) == 1)
printf("%c", &str[0]);
else {
int len = strlen(str);
int lastIndex = len - 1;
char endChar = str[lastIndex];
printf("%c", &endChar);
str[lastIndex] = 0;
print_reverse_str(str);
}
}
int main() {
print_reverse_str("My string");
printf("\n");
print_reverse_str("!ti tog uoy ,siht daer nac uoy fI");
printf("\n");
}
You can not change a string literal.
Character display with printf. E.g printf("%c", character);, not &character
try this.
#include <stdio.h>
#include <string.h>
void print_reverse_str(char *str){
if (*str){
print_reverse_str(str+1);
printf("%c", *str);
}
}
int main(){
print_reverse_str("My string");
printf("\n");
print_reverse_str("!ti tog uoy ,siht daer nac uoy fI");
printf("\n");
}

What is the issue with the following code? [duplicate]

This question already has answers here:
Issue with main arguments handling
(3 answers)
Closed 6 years ago.
I am learning C on my own but this code which seems right to me doesn't works right
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
main()
{
char name[20];
int p,c,m;
printf("Enter your name \n");
scanf(" %s", name);
if ( (name=='luv') || (name='pranav') )
{
printf("Enter your marks in pcm \n");
}
else
{
printf("get lost");
}
getch();
}
I want the correct code to run only if I enter name as luv or pranav but instead what is happening is that no matter whatever name i type it is running the code under else and i am not able to figure out the reason.
I am using codeblocks as compiler.
You cannot compare strings using ==, to compare strings, one has to use strcmp()
strcmp() returns 0 when the strings are same, other wise it returns the difference of those two strings,
So essentially, your code would become,
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // for the strcmp() function
main()
{
char name[20];
int p,c,m;
printf("Enter your name \n");
scanf(" %s", name);
// strings are given inbetween double quotes
// characters are given inbetween single quotes
if ( !(strcmp(name, "luv")) || !(strcmp(name, "pranav")) )
{
printf("Enter your marks in pcm \n");
}
else
{
printf("get lost");
}
getch();
}
NOTE:
1) Use the standard definition of main()
int main(void) //if no command line arguments.
2) Check the return of functions like scanf().
Lots of mistakes in the code.
I am trying to fix and show:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> // to use strcmp
int main(void) // int and void added
{
char name[20];
int p,c,m;
printf("Enter your name \n");
scanf("%19s", name); // no space before % and 19 to limit input
if ( !strcmp(name,"luv") // " instead of ' , and strcmp with operator !
|| strcmp(name,"pranav") == 0 ) // instead of ! you can use == 0
{
printf("Enter your marks in pcm \n");
}
else
{
printf("get lost");
}
getch();
}
main() is not a standard signature. You should use standard int main(void) unless you have some special reason to use non-standard signature.
'luv' and 'pranav' are multiple-character character constant, which have implementation-defined values. You should use string literals and strcmp() function.
name='pranav' is an assignment, and you cannot assign to what is converted from arrays, so this will emit compile error.
You should limit the length to read in order to avoid buffer overflow.
Try this:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char name[20];
int p,c,m;
printf("Enter your name \n");
scanf(" %19s", name);
if ( (strcmp(name, "luv") == 0) || (strcmp(name, "pranav") == 0) )
{
printf("Enter your marks in pcm \n");
}
else
{
printf("get lost");
}
getch();
}
remove #include <conio.h> and getch(); if they are not supported.
Two problems:
Single quotes are used for character constants, not string constants. You need to use double quotes for those.
Strings can't be compared with ==. What you're actually doing is comparing the address of the first element of name with a character constant. Even if you fixed the quotes on the constant, you'd be comparing the address of name with the address of a string constant, which are not the same. To compare strings, you use strcmp, which compares each character in the string.
So what you want is this:
if ( (strcmp(name,"luv") == 0) || (strcmp(name,"pranav") == 0) )
You'll also need to #include <string.h> to use strcmp.

Resources