Making sure the input is a charachter - c

This might be a rookie question, but I need to make sure that the input given by the user is of data type char [%c] or a string [%s].
If it were an integer, I would just do something like this:
int data, x;
do {
printf("Please enter a number: ");
x = scanf(" %d", &data);
getchar();
} while(x!=1);
So I was wondering if there's a similar way to do this, if the input is supposed to be a string or a character. Thanks, Any help would be appreciated!

Avoid to use %c in scanf() because some unexpected character like \r\n will be input.
You can use a char[2] to receive a single character. An \0 will be filled after your string to represent the end of string, so the length of array must be bigger than 1.
An example:
#include <stdio.h>
int main()
{
char data[2];
scanf("%1s", data);
if (data[0] >= 'a' && data[0] <= 'z') // custom your constraint here
{
// legal
printf("legal: %s", data);
}
else
{
// illegal
printf("illegal: %s", data);
}
return 0;
}
While I input b, the data will be "b\0".

part of the answer is if you just want to read only alphabet you can use below.
#include <stdio.h>
#include <ctype.h>
int main()
{
char ch;
do {
printf("enter a char:");
scanf(" %c",&ch);
}while(!isalpha(ch));
printf("%c",ch);
return 0;
}
Update 1:
Just for the completeness and for the FUN part of the programing, have added code here.
This works well (not tested robustly, you can do if you need to) for the single char input or for a string of length 9.
Remember to type the EOF after input is entered in case length of input is < 9.
and read EOF behavior on same line and new line.
#include <stdio.h>
#include <ctype.h>
#define LEN 10
int main()
{
char ch;
char str[LEN] = {0};
int i = 0;
int ret;
printf("enter a char or string(len = 9) and press EOF if len < 9\n");
do {
if(1== (ret = scanf(" %c",&ch)))
{
if(isalpha(ch))
str[i++] = ch;
}
else
printf("scanf:Error (%d)\n", ret);
}while(ret != EOF && ( !isalpha(ch) || i < LEN-1));
str[i] = '\0';
printf("str is %s\n",str);
return 0;
}

Related

scanf() adds character to string

I have this code and it keeps adding what ever the guesses string is to the wordle string when I compare them, resulting in them to never be the same. How can I fix this?
#include <string.h>
int main() {
char wordle[5];
char guesses[5];
int guess = 5;
int value;
printf("Please input a secret 5 letter word:\n");
scanf("%s",wordle);
while (guess != 0){
printf("You have %d tries, please guess the word\n",guess);
scanf("%s",guesses);
value = strcmp(wordle,guesses);
if (value == 0){
printf("you win\n");
break;
}
guess = guess - 1;
}
return 0;
}```
Your program has undefined behavior. You're making two mistakes.
If your user enters 5 characters, it takes 6 characters to store the string. The program would attempt to write a null terminator into wordle[5] which is not a valid index.
Your user could enter any number of letters. You need to make sure they don't overflow your buffer.
#include <stdio.h>
#include <string.h>
int main() {
char wordle[6];
char guesses[6];
int guess = 5;
int value;
int chars_read;
do {
printf("Please input a secret 5 letter word:\n");
chars_read = scanf("%5s%*s\n", wordle);
} while(chars_read != 1 && strlen(wordle) != 5);
while (guess != 0){
do {
printf("You have %d tries, please guess the word\n", guess);
chars_read = scanf("%5s%*s\n", guesses);
} while(chars_read != 1 && strlen(wordle) != 5);
value = strcmp(wordle, guesses);
if (value == 0){
printf("you win\n");
break;
}
guess = guess - 1;
}
return 0;
}
See it in action
scanf, fscanf, sscanf, scanf_s, fscanf_s, sscanf_s
MSC24-C. Do not use deprecated or obsolescent functions
Your strings for wordle and guesses are too short. You need to make room for '\0'. They should be 6 bytes long not 5.
char wordle[6];
char guesses[6];

Character replacement and substitution in C

I'm trying to write a code which will replace character in the string user selects with character he/she does. Eg string london if user picks o and a then the output should be landan.
Here is the code:
#include <stdio.h>
#include <string.h>
#define MAXLEN 100
int function2(char str[], char drop, char sub) {
int i = 0; int num = 0;
while (str != NULL) {
if (str[i] == drop) {
str[i] = sub;
num++;
}
i++;
}
return num;
}
int main() {
char d, s;
char my_string[MAXLEN];
printf("Input your string\n");
scanf("%s", &my_string);
printf("Input character you want to drop\n");
scanf(" %c", &d);
printf("Now character you want to substitute\n");
scanf(" %c", &s);
function2(my_string, d, s);
printf("The string is %s\n", my_string);
return EXIT_SUCCESS;
}
It works up until the point where you actually print the altered string. All I get is Segmentation fault (core dumped). Note that code for function was not mine (I found it on some website, so owner of the original code for function2- Thank you in advance). Any help would be appreciated!
First of all, you should avoid using scanf. If you're interested for the reason and alternatives click here.
But back to your problem
while(str != NULL)
is an infinite loop, because the pointer won't become NULL
while(str[i] != '\0')
should do the trick. It checks each time if you've already arrived at the end of the string.
if (str != null){
while(str[i] != '\0'){
if (str[i] == drop){
str[i] = sub;
num++;
}
i++;
}
}
str is a char array, with str != NULL, you check that the array point to a valid memory address.
With while loop and i++, you loop all characters in array, because the string ends with '\0', you need to stop the loop with while(str[i] != '\0').
Your function runs an infinite loop because str never become NULL, but since i is incremented, str[i] will eventually access memory beyond the end of the string and at some point invalid memory causing a Segmentation fault.
Note also that it is not simple to tell scanf() the maximum number of characters to read into my_string. Using fgets() is much safer and allows a whole phrase to be substituted.
Here is a corrected version:
#include <stdio.h>
#include <stdlib.h>
#define MAXLEN 100
int function2(char str[], char drop, char sub) {
int num = 0;
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] == drop) {
str[i] = sub;
num++;
}
}
return num;
}
int main(void) {
char d, s;
char my_string[MAXLEN];
printf("Input your string\n");
if (!fgets(my_string, MAXLEN, stdin))
return EXIT_FAILURE;
printf("Input character you want to drop\n");
if (scanf(" %c", &d) != 1)
return EXIT_FAILURE;
printf("Now character you want to substitute\n");
if (scanf(" %c", &s) != 1)
return EXIT_FAILURE;
function2(my_string, d, s);
printf("The modified string is %s", my_string);
return EXIT_SUCCESS;
}

How to count number characters of strings without using strlen

I have the task to count the number of letters in random words until "End" is entered. I'm not allowed to use the strlen(); function. That's my solution so far:
#include <stdio.h>
#include <string.h>
int stringLength(char string[]){
    unsigned int length = sizeof(*string) / sizeof(char);
    return length;
}
int main(){
    char input[40];
    
    while (strcmp(input, "End") != 0) {
        printf("Please enter characters.\n");
        scanf("%s", &input[0]);
        while (getchar() != '\n');
        printf("You've entered the following %s. Your input has a length of %d characters.\n", input, stringLength(input));
    }
}
The stringLength value isn't correct. What am I doing wrong?
The %n specifier could also be used to capture the number of characters.
Using %39s will prevent writing too many characters into the array input[40].
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( void)
{
char input[40] = {'\0'};
int count = 0;
do {
printf("Please enter characters or End to quit.\n");
scanf("%39s%n", input, &count);
while (getchar() != '\n');
printf("You've entered the following %s. Your input has a length of %d characters.\n", input, count);
} while (strcmp(input, "End") != 0);
return 0;
}
EDIT to correct flaw pointed out by #chux.
using " %n to record leading space and %n" record total characters this should record the number of leading whitespace and the total characters parsed.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main( int argc, char *argv[])
{
char input[40] = {'\0'};
int count = 0;
int leading = 0;
do {
printf("Please enter characters. Enter End to quit.\n");
if ( ( scanf(" %n%39s%n", &leading, input, &count)) != 1) {
break;
}
while (getchar() != '\n');
printf("You've entered %s, with a length of %d characters.\n", input, count - leading);
} while (strcmp(input, "End") != 0);
return 0;
}
EDIT stringLength() function to return length
int stringLength(char string[]){
unsigned int length = 0;
while ( string[length]) {// true until string[length] is '\0'
length++;
}
return length;
}
Please note that sizeof is evaluated at compile time. So it can't be used to determine the length of a string in run time.
The length of a string is the number of characters until you encounter a null-character. The size of a string is thus one more than the number of characters. This final null-character is called the terminating null character.
So to know the length of a string in run-time, you have to count the number of characters until you encounter a null-character.
Programming this in C is simple; I leave this to you.

Using character for sentinel but receiving pointer error

I'm trying to program a loop that counts characters until it receives a certain sentinel value. The sentinel value is supposed to be a #, but I've also tried a 0 and a Z and had the same response.
When it compiles, I receive "warning: comparison between pointer and integer" for lines 16 (the line that calls the sentinel.)
If I don't define the sentinel, but instead rely on logical operators in the while statement, then I receive no error, but have an endless loop.
Thanks!
#include <stdio.h>
int main()
{
#define SENTINEL '#'
char ch;
int chcount;
printf("Enter your text, terminate with a #:");
scanf("%s", &ch);
chcount = 0;
while (ch != SENTINEL)
{
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount +1;
printf("You have entered %d characters", chcount);
}
}
return(0)
}
With the %s format specifier, scanf expects the address of a char buffer, where the string you type will be copied.
And you gave the address &ch of a single char, which is obviously not enough to contain a "word" from input with its terminating null character.
Moreover, your loop reads no input from the user. Thus the endless loop.
This is because the way you use scanf(), with %s format specifier you are writing to a char*, not the char ch (as you've declared). In order to write to a single char variable, you should use a %c format specifier.
To fix this you should either use f.e. getchar() instead of scanf() or use scanf() (and change ch to char* then) but iterate over scanned string to check whether there is #.
I would recommend the first solution.
The while loop never ends so I changed your while loop.
I tried to change your program to:
#include <stdio.h>
#define SENTINEL '#'
int main()
{
char ch;
int chcount;
printf("Enter your text, terminate with a #:");
chcount = 0;
while ((ch = getchar()) != SENTINEL)
{
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount + 1;
printf("You have entered %d characters\n", chcount);
}
}
return(0);
}
Some issues I found with your code:
scanf("%s", &ch);
It should be
scanf("%c", &ch);
Next, semicolon missing here: return(0);
However, since your aim is:
I'm trying to program a loop that counts characters until it receives a certain sentinel value. The sentinel value is supposed to be a #
I suggest moving your scanf() inside while loop:
#include <stdio.h>
int main()
{
#define SENTINEL '#'
char ch='0';
int chcount;
printf("Enter your text, terminate with a #:");
chcount = 0;
int i=0;
while (ch != SENTINEL)
{ scanf("%c", &ch);
if ((ch >= 'A') && (ch <= 'Z'))
{
chcount = chcount +1;
printf("You have entered %d characters", chcount);
i++;
}
}
return(0);
}
here is a working version of the posted code.
It contains numerous corrections.
Corrections include consistent/usable indentation and logic corrections
Note: not all implementations have the getline() function
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
int sentinelFound = 0;
#define SENTINEL '#'
char* line = NULL;
size_t lineLen = 0;
printf("Enter your text, terminate with a #:");
int chcount;
getline(&line, &lineLen, stdin );
size_t i;
for( i=0; i<lineLen; i++)
{
if( SENTINEL == line[i] )
{
sentinelFound = 1;
break;
}
if ((line[i] >= 'A') && (line[i] <= 'Z')) // only count capital letters
{
chcount = chcount +1;
}
}
free( line );
if( !sentinelFound )
printf( "You did not enter the sentinel character!" );
else
printf("You have entered %d capital characters\n", chcount);
return(0);
} // end function: main

Converting string value to int equivalent in C

Can someone tell me what's wrong with this:
int main()
{
char a[100];
int i = 0;
printf("Enter a number: ");
scanf("%c", &a[i]);
printf("The number you've entered is: %d", a[i] - '0');
}
Brief summary: I am trying to convert a number stored in a char array to its int equivalent. I know in C#, you use the intparse command, but because there isn't such one in C, I do not know what to do. When I input a two digit number, it is only outputting the first digit, of the input char.
strtol sample
#include <stdio.h>
#include <stdlib.h>
int main(){
char str[16], *endp;
int value;
printf("Enter a number: ");
fgets(str, sizeof(str), stdin);
value = strtol(str, &endp, 0);
if(*endp == '\n' || *endp == '\0'){
printf("The number you've entered is: %d\n", value);
} else {
printf("invalid number format!");
}
return 0;
}
If you mean to print ASCII value of char the no need to do a[i] - '0'.
Try this
printf("The number you've entered is: %d", a[i]);
If you are talking about string then first change your scanf statement to
scanf("%s", a);
or better to use fgets library function instead of scanf;
fgets(a, sizeof(a), stdin);
and then use strtol function.
OP method of scanf("%c", &a[i]); only handles a 1 character input.
To read the entire line, suggest using fgets() to read.
The convert to a long using strtol().
Watch for potential errors.
#include <stdio.h>
#include <stdlib.h>
int main(){
// use long to match strtol()
long i;
// size buffer to handle any legitimate input
char a[sizeof i * 3 + 3];
char *endptr;
printf("Enter a number: ");
if (fgets(a, sizeof a, stdin) == NULL) {
// If you would like to handle uncommon events ...
puts("EOForIOError");
return 1;
}
errno = 0;
i = strtol(str, &endptr, 10);
if(*endptr != '\n' /* text after number */ ||
endp == a /* no text */ ||
errno /* overflow */) {
printf("Invalid number %s", a);
}
else {
printf("The number you've entered is: %ld\n", i);
}
return 0;
}
Try
char myarray[5] = {'-', '4', '9', '3', '\0'};
int i;
sscanf(myarray, "%d", &i); // i will be -493
Edit (borrowed from [1]):
There is a wonderful question here on the site that explains and compares 3 different ways to do it - atoi, sscanf and strtol. Also, there is a nice more-detailed insight into sscanf (actually, the whole family of *scanf functions).
#include<stdio.h>
#include<stdlib.h>
int main()
{
char a[100];
int final=0,integer;
int i;
printf("Enter a number: ");
gets(a);
for(i=0;a[i]>='0'&&a[i]<='9';++i)
{
integer=a[i] - '0';
final=final*10+integer;
}
enter code here
printf("The integer is %d ", final);
}

Resources