#include <stdio.h>
int main() {
char a[30];
char b[30];
scanf("%[^\n]s",a);
scanf(" %[^\n]s",b);
printf("%s \n",a);
printf("%s",b);
return 0;
}
Input :
hai
hello
Output :
hai
hello
But I am expecting
hai
hello
How to print leading spaces before hello?
Note that %[…] (a scan set) is a complete conversion specification. The s after it in your code never matches anything, but you can't spot that. The newline is left in the input. You'd have to arrange to read that before using the second scan set — and a space (or newline) in the format string is not the answer to that. Replacing the s with %*c would do the job. But you're probably best off not using scanf() at this point; use fgets() instead.
Using scanf()
#include <stdio.h>
int main(void)
{
char a[30];
char b[30];
if (scanf("%29[^\n]%*c", a) == 1 &&
scanf("%29[^\n]%*c", b) == 1)
{
printf("[%s]\n", a);
printf("[%s]\n", b);
}
return 0;
}
Input:
hai
hello
Output:
[ hai]
[ hello]
Using fgets()
#include <stdio.h>
#include <string.h>
int main(void)
{
char a[30];
char b[30];
if (fgets(a, sizeof(a), stdin) != 0 &&
fgets(b, sizeof(b), stdin) != 0)
{
a[strcspn(a, "\n")] = '\0';
b[strcspn(b, "\n")] = '\0';
printf("[%s]\n", a);
printf("[%s]\n", b);
}
return 0;
}
For the same input, this produces the same output.
You may try fgets:
#include <stdio.h>
int main() {
char a[30];
char b[30];
fgets(a, 30, stdin);
fgets(b, 30, stdin);
printf("%s \n",a);
printf("%s",b);
return 0;
}
Related
first program
#include "stdio.h"
#include "string.h"
void delete_n(char x[]){
int len = strlen(x);
// TITV\n\0 => TITV\0\0
if(x[len-1]=='\n'){
x[len-1]='\0';
}
}
int main(){
char name[50];
printf("your name: ");
fgets(name, sizeof(name), stdin);
delete_n(name);
printf("\nYour name : %s", name);
printf("\nlen = %d", strlen(name));
}
2nd program
#include "stdio.h"
#include "string.h"
void delete_n(char x[]){
int len = strlen(x);
// TITV\n\0 => TITV\0\0
if(x[len]=='\n'){
x[len]='\0';
}
}
int main(){
char name[50];
printf("your name: ");
fgets(name, sizeof(name), stdin);
delete_n(name);
printf("\nYour name : %s", name);
printf("\nlen = %d", strlen(name));
}
why is the length of the string in the 2nd program unchanged as in the first program
both programs aim to delete \n then measure the length of the string
this is images you can see result
enter image description here
The second program does not remove the \n. You can test it yourself
int main(){
char name[50];
printf("your name: ");
fgets(name, sizeof(name), stdin);
delete_n(name);
for(char *p = name; *p; p++)
{
printf("%d ", *p);
}
printf("\n");
}
https://godbolt.org/z/5GWK48rcc
your name: 84 73 84 86 10
The first function is also incorrect. It will fail if the string is 0 chars long. The type for length is also wrong.
void delete_n(char *x)
{
if(x && *x)
{
size_t len = strlen(x);
if(x[len - 1]=='\n')
{
x[len - 1]='\0';
}
}
}
if(x[len]=='\n') is always false, so 2nd program does not remove any '\n'.
Given int len = strlen(x);, x[len] contains '\0'.
Corner cases: x[len-1] risks undefined behavior (UB) should fgets() as len == 0 is possible. Uncommonly, fgets() may first read a _null character.
//if(x[len-1]=='\n'){
// Better as
if(len > 0 && x[len-1] == '\n'){
x[--len]='\0'; // Reduce length
}
or use
// Remove trailing \n if there or not.
x[strcspn(x, "\n")] = '\0';
So I'm supposed to write a program that removes every occurence of substring p from string s, and then print it, using a recursive function, and I'm all out of ideas, so if anyone knows how, let me know. This is as far as I've got:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
void remove(char *s, char *p)
{
char *c=strstr(s,p);
if(c == 0)
return;
}
int main()
{
char s[100], p[50];
printf("Enter string and substring: ");
scanf("%s %s", s, p);
remove(s, p);
printf("New string: %s", s);
getchar();
getchar();
return 0;
}
This should work:
#include <stdio.h>
#include <string.h>
void removestr(char *s, char *p, int len)
{
char *c=strstr(s,p); // strstr returns the address where the substring starts
if(c) {
strcpy(c,c+len); //if the substring was found, copy over it the
//string starting from where the substring ends e.g. (assume
//p="the") thisthestring => thisstring
removestr(s,p,len); //call the function again
}
else
return; //stop if no substring was found
}
int main(void)
{
char s[100], p[50];
printf("Enter string and substring: ");
if(scanf("%99s",s)==1 && scanf("%49s",p)==1) //it's important to check the input
// also %s is vulnerable to overflow, specify the sizes to be safe
removestr(s, p , strlen(p));
printf("New string: %s", s);
getchar();
getchar();
return 0;
}
This is part of my main function, the problem is: if my input for translation is given more than one word the program doesn't work properly, any idea about how can I fix that?
int main() {
struct node *temp;
char str[1000];
char word[MAXP];
char translation[MAXT];
char option[15];
while (1) {
str[0] = '\0';
word[0] = '\0';
translation[0] = '\0';
scanf("%[^\n]%*c", str);
sscanf(str, "%s %s %s", option, word, translation);
}
...
You can use fgets to read each input. Then sscanf to scan for the first two sub-strings. Using the %n specifier, the number of characters scanned can be captured to allow you to use strcpy from that index.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char end = '\0';
char str[1000];
char word[1000];
char translation[1000];
char option[15];
int used = 0;
int scanned = 0;
while(1){
str[0]='\0';
word[0]='\0';
translation[0]='\0';
fgets ( str, sizeof ( str), stdin);
str[strcspn ( str, "\n")] = '\0';//remove newline
scanned = sscanf(str, "%14s%999s%c%n", option, word, &end, &used);
if ( scanned >= 1) {//one sub string scanned
printf ( "%s\n", option);
}
if ( scanned >= 2) {//two sub strings scanned
printf ( "%s\n", word);
}
if ( scanned == 3) {//two sub strins and a character scanned
strcpy ( translation, &str[used]);
printf ( "%s\n", translation);
}
}
return 0;
}
I am trying to remove the leading zeros of the number user enters so 000002 will turn into 2
However, I am getting an error saying Segmentation fault (core dumped)
#include <stdio.h>
#include <string.h>
int main()
{
char *str;
scanf("%c", *str);
int n;
if( ( n = strspn(str, "0" ) ) != 0 && str[n] != '\0' ) {
printf("String without leading zeros is %s \n", &str[n]);
} else {
printf("No leading zeros in %c \n", str);
}
return 0;
}
Change %c to %s and *str to str.
You don't need to use the * when scanning.
Besides the errors pointed out in the previous answers, scanf("%d") will take care of removing leading zeroes for you. Do the test:
#include <stdio.h>
int main()
{
int a;
scanf("%d", &a);
printf("%d\n",a);
return 0;
}
And if you absolutely need a string, just convert with sprintf:
#include <stdio.h>
int main()
{
char str[256];
int a;
scanf("%d", a);
sprintf(str, "%d", a);
puts(str);
return 0;
}
remove * from this:
scanf("%c", *str);
and change %c to %s because you are scanning a string not character
As #user3291093 has said, you need to change %c to %s since you are reading a string/char array and not a single character.
Also you need to malloc an area to store the array. For example:
char *str = NULL;
str = malloc(100*sizeof(char));
scanf("%s", str);
The compiler should have warned about this problem. Insure warnings are enabled.
char *str;
scanf("%c", *str); // "%c" does not match `*str` for scanf()
Suspect OP wanted something like
char str[100];
if (scanf("%99s", str) == 1) Continue_Along_the_Happy_Path();
An alternative fgets() solution.
char str[100];
fgets(str, sizeof str, stdin);
char *p = buffer;
while (*p == '0') p++;
if (p != buffer) {
printf("String without leading zeros is %s", p);
} else {
printf("No leading zeros in %s", str);
}
#include<stdio.h>
#include<string.h>
int main()
{
char buffer[32];
char c;
int i;
printf("input: ");
fgets(buffer, 32, stdin);
printf("items filled: %d\n", sscanf("%c%d\n", &c, &i));
printf("%c%d\n", c, i);
return 0;
}
When typing a character followed by a number "f7", im expecting "f" to go into variable c, and "7" to go into variable i. For some reason, sscanf() fails to fill both, and I'm getting their initial garbage values. What am I doing wrong?
Actual sscanf() signature is this :
int sscanf(const char *str, const char *format, ...);
check your signature of sscanf() you have used wrong signature
It should be
sscanf(buffer,"%c%d\n", &c, &i);
You're not using buffer in the sscanf() call, so they're not getting filled.
int i;
char c;
char buffer[32];
fgets(buffer, 32, stdin);
sscanf(buffer, "%c%d", &c, &i)
^
you're missing this part
Do not use sscanf. The correct way to do the parsing you are trying to do is
#include <stdlib.h>
#include <stdio.h>
// ...
char *endptr, buffer[32];
char c;
int i;
// ...
fgets(buffer, 32, stdin);
c = buffer[0];
i = strtol(buffer+1, &endptr, 10);
if (endptr == buffer+1 || (*endptr != '\0' && *endptr != '\n')) {
puts("invalid input");
return 1;
} else {
printf("%c%d\n", c, i);
return 0;
}
sscanf("%c%d\n", &c, &i) is never told to look in the buffer for c and i, it looks like you are making this call incorrectly.