Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I'm having this simple problem that I can't get over with.
int B;
char *a = (char*)malloc(1000*sizeof(char);
scanf("%[^\n]s", a);
printf("%c\n", a[0]);
B = strlen(a);
printf("%d\n", B);
If I put an entry like:
" abc "
a[0] = 'a' and B = 5 (the "abc" plus the last two spaces). I want to count the first three spaces, how do I do it?
You could simply read the entire line using fgets() instead of using scanf(), which generally skips whitespace unless you explicitly capture it.
int B;
char *a = malloc(1000 * sizeof*a); /* don't cast malloc */
if (fgets(a, 1000, stdin) == NULL) {
fputs("Could not read input\n", stdout);
}
else {
printf("%c\n", a[0]);
B = strlen(a);
printf("%d\n", B);
}
If you want to trim the leading space, you can try this:
void trim_left(char *p);
int main()
{
int B;
char *a = (char*)malloc(1000*sizeof(char));
scanf("%[^\n]s", a);
printf("%c\n", a[0]);
trim_left(a);
B = strlen(a);
printf("%d\n", B);
printf("a = %s\n", a);
return 0;
}
void trim_left(char *p) {
int i = 0;
while (p[i] && isspace(p[i])) {
i++;
}
memmove(p, p+i, strlen(p+i));
p[strlen(p+i)] = '\0';
}
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I am trying to make a program which prints all the possible permutations of the string "A?B?AB?" via replacing question marks with A or B. I can't understand why my code works the way it does and need help improving it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* rec(char s[8]){
int i=0;
for(;i<strlen(s);i++){
if(s[i]=='?'){
s[i]='A';
rec(s);
s[i]='B';
rec(s);
s[i]='?';
}
}
for(int k=0;k<strlen(s);k++){
if(s[k]=='?')
i=-1;
}
if(i!=-1)
printf("%s\n",s);
return s;
}
int main(){
char s[8]="A?B?AB?";
rec(s);
}
This should do the trick:
#include <stdio.h>
#include <string.h>
void rec(char *str, int size, int depth){
for (int i = depth; i < size; i++){
if (str[i] == '?'){
str[i] = 'A';
rec(str, size, i + 1);
str[i] = 'B';
rec(str, size, i + 1);
str[i] = '?';
return;
}
}
printf("%s\n", str);
}
int main(){
char s[8] = "A?B?AB?";
rec(s, strlen(s), 0);
}
It's much like August's solution, but I did decide to do some looping until it found the next question mark. That should avoid having too big of a callstack, which could lead to stack overflow, with really big strings. (Note: I didn't test it, so there could still be some minor problems)
You only need to look at one character at a time. Try this:
#include <stdio.h>
void PrintPermutationsFrom(int i, char s[])
{
if (s[i] == '?') {
s[i] = 'A';
PrintPermutationsFrom(i + 1, s);
s[i] = 'B';
PrintPermutationsFrom(i + 1, s);
s[i] = '?';
} else if (s[i] != '\0') {
PrintPermutationsFrom(i + 1, s);
} else {
puts(s);
}
}
void PrintPermutations(char s[])
{
PrintPermutationsFrom(0, s);
}
int main(void)
{
char s[] = "A?B?AB?";
PrintPermutations(s);
return 0;
}
I can't understand why my code works the way it does and need help improving it.
The rec code does simply too much in that after having replaced a ? with both A and B and called itself, it continues iterating over s and generates further output. That is too much because after the first found ?, the recursive invocations already have handled all following ? and generated all arrangements. To correct this, just insert a break; after s[i]='?';.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I have tried to make a simple program in C to count the number of char in a string. But it only counts the first word, if i leave any space it stops
#include <stdio.h>
int main() {
char str1[100];
int i = 0, count = 0;
printf("enter string:\n");
scanf("%s", str1);
while (str1[i] != '\0') {
i++;
count++;
}
printf("total number of char %d", count);
return 0;
}
scanf("%s", str1) reads a single word and leaves the separator (any sequence of white space) and the rest of the input line in the stdin buffer. If you want to count the number of characters in the full input line, use fgets() to read a full line and stop on the newline ('\n') in addition to the end of string ('\0').
Here is a modified version:
#include <stdio.h>
int main() {
char str1[100];
int i;
printf("enter string:\n");
if (fgets(str1, sizeof str1, stdin)) {
for (i = 0; str1[i] != '\0'; i++) {
if (str[i] == '\n') {
str[i] = '\0';
break;
}
}
printf("total number of chars: %d\n", i);
}
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to make something that changes only one character in a string. As an example.
char str[10];
fgets(str,10,stdin); //input hello
// do something to change 'hello to he1lo'
All I can found is some functions change all of same letters.
You can modify by searching through a string then replace it with new char.
#include<stdio.h>
int replace(char *str,const char old_char,const char new_char,const int replace_char_count) {
int count_replaced_chars = 0;
for(int i = 0; str[i] != '\0';i++) {
if(str[i] == old_char) {
str[i] = new_char;
count_replaced_chars++;
if(count_replaced_chars == replace_char_count)
return 0;
}
}
return -1;
}
int main()
{
char str[10];
char new_char = '1';
char old_char = 'l';
int replace_char_count = 0;
printf("Enter the string in which chars to be replaced\n");
fflush(stdout);
fgets(str,10,stdin); //input hello
printf("Enter how many chars to replaced\n");
fflush(stdout);
scanf("%d",&replace_char_count);
if(!replace(str,old_char,new_char,replace_char_count))
printf("Successfully %d number of replaced char\n",replace_char_count);
else
printf("char not found\n");
printf("%s\n",str);
}
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
How would one go about adding integers together like this.
Say you start with 1, then you add 2. So you have 12, next add 3, so you have 123. And so on.
I would just concatenate but I'm not allowed to use strings in this program.
Using some unusual math (based on the mechanisms of the decimal system) to make the desired variation of adding:
Code:
#include <stdio.h>
int main(void)
{
int i;
int number=0;
for (i=1; i<5; ++i)
{
number=number*10 + i;
printf("%d\n", number);
}
return 0;
}
Output:
1
12
123
1234
Like this?
#include <stdio.h>
int main() {
int a = 4, b = 5, c = 6, d = 7;
printf("a+b=%d\n",a*10+b);
printf("a+b+c=%d\n",(a*10+b)*10+c);
printf("a+b+c+d=%d\n",((a*10+b)*10+c)*10+d);
return 0;
}
This can be typical case to use realloc
char *mystr = NULL;
char *temp;
char c, ch;
short count = 0;
do {
printf("Enter character : ");
scanf(" %c", &c); // Better use sscanf or fgets, scanf is problematic
temp = (char*)realloc(mystr, (++count) * sizeof *mystr);
if (NULL != temp) {
mystr = temp;
mystr[count - 1] = c;
}
printf("Do you wish to continue: ");
scanf(" %c", &ch);
} while ('y' == ch);
// Since and since you don't have a null terminated string, do
for (int i = 0; i < count; i++)
printf("%c", mystr[i]);
printf("\n");
free(mystr); // Freeing the memory
getch();
Note : And you don't have strings in this program ;)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am currently learning C as part of a course and I have a task to reverse the order of a number without using any arithmetic (wording of the task).
I currently have this:
#include <stdio.h>
int main()
{
int n, reverse = 0;
printf("Enter a number to reverse\n");
scanf("%d", &n);
while (n != 0) {
reverse = reverse * 10;
reverse = reverse + n_10;
n = n / 10;
}
printf("Reverse of entered number is = %d\n", reverse);
return 0;
}
But this solution uses arithmetic. How can I alter this to complete the task?
From the exercise sheet, "though even at three
digits the naïve approach quickly grows unmanageable with arithmetic (try it!).
Your goal: Design 3-digit solution without using any arithmetic.(Hint: use scanf.)"
The wording of the question was confusing but to be clear the question is:
How do you reverse the order of a number, e.g. 123456789 to 987654321, without without using math. So the solution is to scanf to read the number and swap the numbers in a loop as the answer below.
You can just read it in as a string.
int main()
{
char str[80];
fgets(str, 80, stdin);
strrev(str);
printf("%s\n", str);
}
void strrev(char *str)
{
char *end, tmp;
end = str + strlen(str) - 1;
for (; end > str; --end, ++str) {
tmp = *end;
*end = *str;
*str = tmp;
}
}
I don't know if you consider pointer arithmetic to be "arithmetic"
Read as series of short strings that can hold 1 digit and then print in the reserve order.
char buf[10][2] = { 0 };
scanf("%1[0-9]%1[0-9]%1[0-9]%1[0-9]%1[0-9]%1[0-9]%1[0-9]%1[0-9]%1[0-9]%1[0-9]",
&buf[0], &buf[1], &buf[2], &buf[3], &buf[4],
&buf[5], &buf[6], &buf[7], &buf[8], &buf[9]);
printf("%s%s%s%s%s%s%s%s%s%s",
buf[9], buf[8], buf[7], buf[6], buf[5],
buf[4], buf[3], buf[2], buf[1], buf[0]);
Obviously limited to max 10 digits.
A recusive approach, inpsired by #Nisse Engström. Works for any reasonable length input.
void pr(void) {
char digit[2];
if (scanf("%1[0-9]",digit)) {
pr();
printf("%s",digit);
}
}
int main(void) {
pr();
return 0;
}
Input
012345678901234567890123456789
Output
987654321098765432109876543210
Of course if (scanf("%1[0-9]",digit)) { should be if (scanf("%1[0-9]",digit) == 1) { to cope with EOF but that then may be "arithmetic".
Using scanf():
int
scanf (){
int
stdin =
getchar ();
return
stdin <0||
stdin ==
'\n'?
stdin :(
scanf (),
putchar (
stdin )),
'\n';}
int
main (){
putchar (
scanf ()
);}
Read integer, use sprintf() function to convert it into string and then use the library function strrev() to reverse the string.