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);
}
Related
#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;
}
After confirming input Segmentation fault pops up and I can't see why. I was told to use fgets and sscanf in loop to read an undefined number of floats from terminal input and this is what i came up with..
Code
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define EMPTY 3.141592
#define BUFFERSIZE 100
void removeSubstring(char *s,const char *toremove){
while( (s=strstr(s,toremove)) )
memmove(s,s+strlen(toremove),1+strlen(s+strlen(toremove)));
}
int main(){
int x=0;
int y=0;
float a[BUFFERSIZE] = {EMPTY};
char buffer[BUFFERSIZE] = {EMPTY};
char *pos;
char *start = 0;
int space = ' ';
printf("Input: ");
fgets(buffer, BUFFERSIZE, stdin);
while(x< BUFFERSIZE){
sscanf(buffer, "%f ", &a[x]);
pos = strchr(buffer, space);
removeSubstring(start, pos);
x++;
}
printf("Saved input: ");
while(y<BUFFERSIZE && a[y] != EMPTY){
printf("%.2f", a[y]);
y++;
}
printf("\n");
return 0;
}
Edit: Increased both array sizes and removed feof
Almost certainly the problem is in the line pos = strchr(buffer, space);. If the input does not contain a space character, then pos is set equal to NULL. Passing a NULL to strstr() will likely result in a SEGV.
I wrote a function that removes a given char from a string.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* input_long(void);
void removeChar(char str[], char ch);
void main()
{
char *str, ch;
str=input_long();
printf("\nplease enter a char to be removed from the string: ");
scanf("%c", &ch);
removeChar(str, ch);
printf("the string after the removal is %s \n", str);
free(str);
}
void removeChar(char str[], char ch) //this function removing a given char from a given string//
{
int i,j = 0;
for (i = 0; str[i] != '\0'; i++) //looping the string until its end//
{
if (str[i] != ch) //by that we ensure the char will be erased from the string //
{
str[j] = str[i];
j++;
}
}
str[j]='\0'; //the end of the new string after the whole process//
}
char* input_long(void) //this function gets a string dynamically allocated//
{
char tempstr[80], *str;
printf("enter a string\n");
gets(tempstr);
str=(char*)malloc((strlen(tempstr)+1)*sizeof(char));
strcpy(str,tempstr);
return str;
}
My code didn't run well; when I ran it, it seemed to lead the statement which asks the user to enter a string. I repaired the code by adding _flushall() , and now the code is running well:
_flushall
str=input_long();
printf("\nplease enter a char to be removed from the string: ");
scanf("%c", &ch);
I don't really understand why adding that statement indeed repaired it.
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;
}
#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.