Convert Lowercase Letters to Uppercase in C Language - c

I'm working on a simple piece of program to incorporate into a larger program. I'm following what my professor has already taught.
#include <stdio.h>
int main()
{
char letter;
printf("Please Enter a Lower Case Letter:");
scanf("%s", letter);
if (letter >= 'a' && letter <= 'z')
letter = letter - 'a' + 'A';
printf("%s", letter);
return 0;
}
The initial 'Please Enter a Lowercase Letter:' appears but after inputting a letter the return is (null). Any help is appreciated.

Maybe try getchar() instead of scanf(). Links to C reference for getchar() and scanf()
#include <stdio.h>
int main()
{
char letter;
printf("Please Enter a Lower Case Letter:");
letter = getchar();
if (letter >= 'a' && letter <= 'z')
letter = letter - 'a' + 'A';
printf("%c", letter);
return 0;
}

scanf("%s", letter); will read a string and copy it to the memory letter points to so pass the pointer of letter instead (scanf("%s", &letter);).
Also use %c instead of %s if you only need one character otherwise scanf can write some unwanted bytes to adjacent memory.
Likewise for the last printf line.

Related

Alphabet and code ascii

I am trying to create a program in c language that gets a number and a string, for example the number is 3 and the string is "Zig".
The output should be alphabet's codes + the number. If the character's number goes more than "Z" (90) it should start from "A" again. Similarily, for lower case characters, when going beyond "z" start again from "a".
input : "Zig"
number : 3
output : "Clj"
I have a problem with the part that it should start from A(for capitals) & a again.
this is my code now ! I have a problem with rotation part how to start from A or a again :)
char a[50];
int n,i;
printf("enter your number:\t");
scanf("%d",&n);
printf("enter your string:\t");
fflush(stdin);
gets(a);
while('A'<= a[i] <= 'Z'){
if(a[i]+n > 'Z'){
}
else{
a[i]=a[i]+n;
}
i++;
}
while('a' <= a[i] <= 'z'){
if(a[i]+n > 'z'){
}
else{
a[i]=a[i]+n;
}
i++;
}
printf("string:\n");
puts(a);
}
Fixed Bug
#include <stdio.h>
#include <strings.h>
int main(){
char str [80];
int number;
printf ("Enter your string: ");
scanf ("%79s",str);
printf ("Enter your number: ");
scanf ("%d",&number);
for(int i= 0; i < strlen(str);i++){
str[i]+=number;
while(str[i]>'Z' && str[i] < 'a'){
str[i] = 'A'+ str[i] - 'Z';
}
while(str[i]>'z'){
str[i] = 'a'+ str[i] - 'z';
}
}
printf("%s",str);
}
I have a problem with the part that it should start from A(for capitals) & a again
When you get to Z (decimal 90) you can easily subtract 25 (ASCII char "EM") to get back to A (decimal 65). Similarly you can repeat subtraction of 25 when you reach z and it will cycle back to

C Rotating String Program?

#include <stdio.h>
int main()
{
int rotation, i=0;
char str[80]={0};
printf("Enter Text: ");
scanf("%[^\n]", str);
printf("\"");
printf("Enter Rotation: ");
scanf("%i", &rotation);
while(str[i])
{
if (str[i] >= 'a' && str[i] <= 'z')
printf("%c\n", 'a' + (str[i] - 'a' + rotation)%26);
else
printf("%c\n", str[i]);
i++;
}
return 0;
}
Have a hard time understanding this line of code (printf("%c\n", 'a' + (str[i] - 'a' + rotation)%26); )
Can anyone just write a brief explanation quickly it would help me
The program is taking the user's input text and on a per character basis rotating it through the alphabet based on the number entered. It works because of the ASCII table.
The line in question takes the character a user entered, offsets it by 'a' (which equals 91 in ASCII), adds in the rotation factor then performs modulo 26 on the result (how many characters are there in the alphabet again?) to make sure the result is still a lowercase character.
I bet you could find a good way to break this program :)

user input in c

I cannot figure out how to take away a letter dependent on user input. For example, if I enter b the code should output cdefghijklmno... If I enter c the code should output defghijklmno... I just don't know where to do the math?
#include <stdio.h>
int main(void) {
char letter;
printf("Type one letter of alphabet and following letters will appear (lowercase): ");
scanf_s("%c", &letter);
if (letter >= 'a' && letter <= 'z'){
printf("%c, valid entry!\n\n", letter);
for (letter = 'a'; letter <= 'z'; ++letter){
printf("%c ", letter);
}
} else {
printf("%c, invalid entry.\n\n", letter);
}
return 0;
}
In this line:
for (letter = 'a'; letter <= 'z'; ++letter){
why do you start at a? Dont you want to start one after the user input letter, which is stored in letter?
So do that:
for (++letter; letter <= 'z'; ++letter){
if I enter b the code should output cdefghijklmno... If I enter c the code should output defghijklmno... I just don't know where to do the math
As #Ben has suggested, change your for loop to :
for (++letter; letter <= 'z'; ++letter)
{
printf("%c ", letter);
}
The reason of using ++letter is to print the alphabets which come after the entered letter up to z
This would do the job
further, from OP's comment:
To give the user repeated chances if input is incorrect
using a while loop would be helpful :
while( (scanf(" %c",&letter) == 1) && letter!='*')
{
if (letter >= 'a' && letter <= 'z')
{
printf("%c, valid entry!\n\n", letter);
for (++letter; letter <= 'z'; ++letter)
{
printf("%c ", letter);
}
printf("\n\n");
// break;
// place the above break if you want to stop scanning after first successful entry
}
else
{
printf("%c, invalid entry.\n", letter);
printf("try again\n\n");
}
}
this loop would continue till the use enters * (an asterik)
Note : while( (scanf(" %c",&letter) == 1) && letter!='*') here, we check whether scanf() successfully scanned or not and whether the scanned element is * or not
sample input :
1
2
a
t
3
*
output :
Type one letter of alphabet and following letters will appear (lowercase):
1, invalid entry.
try again
2, invalid entry.
try again
a, valid entry!
b c d e f g h i j k l m n o p q r s t u v w x y z
t, valid entry!
u v w x y z
3, invalid entry.
try again
*
ended
Try to increment the letter variable by 1 in for loop parameter:
for (letter += 1; letter <= 'z'; ++letter){
printf("%c ", letter);
}
You can also do it this way:
#include <stdio.h>
#include <ctype.h>
#define MAX 122
int
main(void) {
char letter;
int i;
printf("Type one letter of the alphabet(lowercase): ");
scanf("%c", &letter);
if (islower(letter)) {
printf("%c, valid entry!\n\n", letter);
for (i = letter + 1; i <= MAX; i++) {
printf("%c", i);
}
} else {
printf("%c, invalid entry.\n\n", letter);
}
return 0;
}

Run-Time Check Failure #2 - Stack around the variable 'char' was corrupted

#include <stdio.h>
int main()
{
char ch;
int i;
for (i = 1; i <= 5; i++) {
printf("Enter a Character: ");
scanf("%s", &ch);
if ((ch >= '0') && (ch <= '9'))
printf("The character is a numeral\n");
else if ((ch >= 'A') && (ch <= 'Z'))
printf("The character is in upper case\n");
else if ((ch >= 'a') && (ch <= 'z'))
printf("The character is in lower case\n");
else
printf("The character is a special character\n");
}
return 0;
}
I want to read a character input from user and display the character type. However everytime after running the last scan of the program, it will have a debug error. I am using Visual Studios C++ 2010 Express.
Run-Time Check Failure #2 - Stack around the variable 'ch' was corrupted.
Please help!
To scan a char use
scanf("%c", &ch);
Using wrong format specifier will lead to undefined behavior.
Please make sure you ignore the newline char and do it by placing a space before %c
scanf(" %c", &ch);
The space before the %c will make sure that the newline char in the buffer gets ignored. i.e space gobbles the newline char.
In scanf,
If you want to the single character you have to use the control string %c. %s for
getting the string.
So change the scanf into
scanf(" %c", &ch);

Double ask for input if wrong input is given

My functions:
//Check Character Case(Upper/Lower Case). If Upper Convert to Lower Case.
char checkLetterCase(char letter){
if(letter >= 65 && letter <= 90){
letter = tolower(letter);
return letter;
}
else if(letter >= 97 && letter <= 122){
return letter;
}
else{
return 0;
}
}
//Add the Specified Letter by Creating a New Node in the Letter List defined
void addLetter(letterListT *letListHead, char letter){
letterListT *newNode;
newNode = (letterListT *)malloc(sizeof(letterListT));
//Check Case(lowe/upper)
letter = checkLetterCase(letter);
//This may only occur on user input
while(letter == 0){
printf("Guess a letter: ");
scanf("%c", &letter);
letter = checkLetterCase(letter);
}
newNode->letter = letter;
newNode->nxt = letListHead->nxt;
letListHead->nxt = newNode;
}
My call in main:
addLetter(unusedLetList, i=0);
and this is my outpun on run:
Guess a letter: 6
Guess a letter: Guess a letter: t
and my question is: why when i put a non character i get the "Guess a letter" message twice?
Because after pressing <enter>, there's an extra newline character that is not (yet) consumed by scanf(). Make a call to getchar() to get rid of that:
letter = checkLetterCase(letter);
if (letter == 0) getchar();
By the way, your checkLetterCase() function is broken on non-ASCII systems. Use this instead:
#include <ctype.h>
char checkLetterCase(char letter)
{
return isalpha(letter) ? letter : 0;
}
A little suggest about avoiding nasty things with buffers, just don't use scanf.You could use it and clean the buffer, of you could use fgets.
I suppose that you could learn how to use it by reading the manual, but I'll make you an example:
while(letter == 0)
{
char buffer[10];
printf("Guess a letter: ");
fgets(buffer,10,stdin);
if(strlen(buffer)==2)
letter = checkLetterCase(buffer[0]);
}

Resources