I've printed a string of "+" symbols based on two given values(N, M). Now I'm trying to figure out how to replace characters at random in said string based on a third given value(K). The characters are stored in a string(l). I think I have to use the replace function but I don't know how(hence why it's in a comment for now). Any help is appreciated.
#include <stdio.h>
unsigned int randaux()
{
static long seed=1;
return(((seed = seed * 214013L + 2531011L) >> 16) & 0x7fff);
}
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
char s[1000];
int N, M, K, l;
printf("N: ");
scanf("%d",&N);
printf("M: ");
scanf("%d",&M);
printf("K: ");
scanf("%d",&K);
printf("\n");
gets(s);
l=strlen(s);
/* Mostre um tabuleiro de N linhas e M colunas */
if(N*M<K){
printf("Not enough room.");
}else if(N>40){
printf("Min size 1, max size 40.");
}else if(M>40){
printf("Min size 1, max size 40.");
}else{
for(int i=0; i<N; i++)
{
for(int j=0; j<M; j++)
{
printf("+", s[j]);
}
printf("\n", s[i]);
}
for(int l=0; l<K; l++)
{
/*s.replace();*/
}
}
return 0;
}
There is too much unexplained complexity and unknowns in your program to enable a corrective answer. But this shows how to replace a textual string's character at random, with a numeral.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main(void)
{
char str[] = "----------";
int len = strlen(str);
int index;
int num;
srand((unsigned)time(NULL)); // randomise once only in the program
printf("%s\n", str); // original string
index = rand() % len; // get random index to replace, in length range
num = '0' + rand() % 10; // get random number, in decimal digit range
str[index] = num; // overwrite string character
printf("%s\n", str); // altered string
return 0;
}
Program sessions:
----------
-3--------
----------
-----0----
----------
--------6-
Arguably it would be better to use size_t types, but for the limited range of the example, will suffice.
Related
There seems to be an overflow phenomenon depending on the declaration of n before and the declaration of n after Can you tell me the detailed reason?
Operable code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, sum=0;
char n, DNA[11]={};
printf("용의자의 DNA정보를 입력하세요. :");
scanf("%s", DNA);
for(i=0; i<10; i++)
{
n = DNA[i];
sum += atoi(&n);
}
if (sum%7 == 4)
printf("범인");
else
printf("일반인");
}
Inoperable code
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i, sum=0;
char DNA[11]={}, n;
printf("용의자의 DNA정보를 입력하세요. :");
scanf("%s", DNA);
for(i=0; i<10; i++)
{
n = DNA[i];
sum += atoi(&n);
}
if (sum%7 == 4)
printf("범인");
else
printf("일반인");
*Input conditions are up to 10 characters
The both programs have undefined behavior.
For starters you may not use empty braces to initialize an array in C (opposite to C++).
char n, DNA[11]={};
Secondly, the function atoi expects a pointer to a string. However you are using a single character n
sum += atoi(&n);
If you want for example to add digits of the entered number then write
for(i=0; DNA[i] != '\0'; i++)
{
n = DNA[i];
sum += n - '0';
}
Also you need guarantee that the length of the entered string is not greater than 10
scanf("%10s", DNA);
I need to find all suffix starting with a character X. For example, for int suffix (char str [], char c) when the word is ababcd and the letter b it should return:
babcd
bcd
and the number 2.
This is my code:
#include <stdio.h>
#include <string.h>
int main()
{
char c;
char str[128];
int counter=0;
printf ("Please enter charachter and a string \n");
scanf("%c %s",&c,str);
counter = my_suffix(str,c);
printf("The string has %d suffix \n",counter);
return 0;
}
int my_suffix(char str[],char c) {
int counter = 0;
for (int i=0; i < strlen(str); i++)
{
if (str[i] == c)
{ puts(str+i);
counter++;
}
}
return counter;
}
I couldn't find why it's not running,
Thanks!
Your code is fine you should just written following method above int main()
int my_suffix(char str[],char c){...}
I am trying to write a function to divide a string in half but after the initial input it does not output anything. My goal is to scan a year and save the first two number and the last two numbers. This is the code:
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char scan_year2() {
char year_number;
scanf("%s", year_number);
return year_number;
return 0;
}
// Function to print n equal parts of str
void divideString(char *str, int n) {
int str_size = strlen(str);
int i;
int part_size;
if (str_size % n != 0) {
printf("Invalid Input: String size");
printf(" is not divisible by n");
return;
}
part_size = str_size / 2;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
printf("\n");
printf("%s", str[i]);
}
}
int main() {
char year_number;
scan_year2();
char str = year_number;
divideString(str, 2);
getchar();
return 0;
}
Assuming that a year is at least a 3-digit number, the best way to treat it is to treat it as a number, not as a string:
...
int year;
scanf("%d", &year);
int first = year / 100;
int last = year % 100;
printf("%d %d\n", first, last);
...
dont ignore compiler warnings, it must be complaining at you about this
char scan_year2() {
char year_number;
scanf("%s", year_number);
return year_number;
return 0;
}
you try to return twice.
Also
part_size = str_size / 2;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
printf("\n");
printf("%s", str[i]);
}
is not going to give you the correct output. YOu are outputing the string each time. IE if str = "1923" then you will get
1923923
232
You should do
part_size = str_size / 2;
for (i = 0; i < str_size; i++) {
if (i % part_size == 0)
printf("\n");
printf("%c", str[i]);
}
to only output one char at a time
I haven't been coding for long (just 2 months).
My question has to do with the iteration of the counter in loops. Below is my program
with while:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int cnt=0;
int match;
int position;
int tries=0;
char letter;
int main()
{FILE *Difficult= fopen("/GODlvl.txt", "r");
char singl[19];
if (Difficult==NULL){
return -1;}
for(int i = 0; i < rnd1; i++)
fgets(singl, 21, Difficult);
int DashN1= strlen(singl);
printf("%s", singl);
for (int i=0; i<DashN1-1; i++)
{
singl[i]='_';
singl[DashN1]='\0';
}
for (int i=0; i<DashN1; i++) /**it adds an extra character ..possibly a space..**/
{
printf(" ");
printf(" %c", singl[i]);
}
do{
scanf(" %c", &letter);
if(letter==singl[cnt])
{
match=1;
position=cnt;
printf("match found");
}
if (position==cnt)
{
singl[position]=letter;
printf(" %s", singl);
}
cnt++;
tries++;
}
while(tries!=8);
}
the do loop runs starting from 0, and iterates after every step. The problem with this is with the if conditions; they don't test for any arbitrary element in the char array (singl). How can i edit this code(whether the if conditions or the loop) to run for arbitrary index.
After reading the user input for letter, you can request a random index for use in singl, just by doing:
srand((unsigned)time(NULL));
cnt = rand() % DashN1;
This means that your do loop, should look like:
do{
scanf(" %c", &letter);
srand((unsigned)time(NULL));
cnt = rand() % DashN1;
if(letter==singl[cnt]){
//....
}
}while(/*...*/);
Make sure that you do:
#include<stdlib.h>
in order to access srand and rand
I want to have unknown amount of inputs in a single line. For example, user can input:
"ans: 1 2 3 4 5"
and scanf() will store these five numbers to an array. The problem is that the program don't know how many input will there be.
#include <stdio.h>
int main()
{
int i;
int input[4];
scanf("ans: " for(i = 0, i < 3,i++){scanf(" %d", &input[i]);};
return 0;
}
Sorry I'am totally new to coding, what will be the proper way to write this? Or is this impossible?
Thanks :)
Use fgets() and sscanf() with "%n"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char input[100];
int arr[10];
//fgets(input, sizeof input, stdin);
strcpy(input, "1 2 42 56 -3 0 2018\n"); // fgets
char *pi = input;
int tmp, pp, i = 0;
while (sscanf(pi, "%d%n", &tmp, &pp) == 1) {
if (i == 10) { fprintf(stderr, "array too small\n"); exit(EXIT_FAILURE); }
pi += pp;
arr[i++] = tmp;
}
printf("got this ==>");
for (int k = 0; k < i; k++) printf(" %d", arr[k]);
puts("");
}
You asked this question way round.
You can achieve what you expect by putting scanf inside of a loop.Even you can ask user to give how many inputs he want to enter.
#include <stdio.h>
int main()
{
int i;
int input[4];
printf("Enter the number of inputs you want to give : ");
scanf("%d", &n);
for(i = 0; i < n;i++)
{
printf("Enter the input number %d : ",i);
scanf("%d", &input[i]);
}
return 0;
}