Random string from an array in c - c

I'm kind of new to C, but not to programming. I'm trying to create a program that takes an input and replies with a random string that's already saved in an array (for example).
I'm not trying to create a random string, I want them to be "fixed", like in Java:
String [] sa;
sa[0] = "Hello, World";
sa[1] = "Hi dude!";

const char *sa[]={"Hello, World","Hi dude!"};
Then you can do
return sa[i];
The return value is char *
Just make sure i is within bounds

#include <stdio.h>
#include <stdlib.h>
int main() {
const char *messages[] = {
"Hello!",
"How are you?",
"Good stuff!"
};
const size_t messages_count = sizeof(messages) / sizeof(messages[0]);
char input[64];
while (1) {
scanf("%63s", input);
printf("%s\n", messages[rand() % messages_count]);
}
return 0;
}

It's not clear as to what you exactly want, but here is a brief description of how strings work in C.
There are no String like data type in C as you have in Java. You have to use array of characters. For an array of strings, you have to use two dimensional array of characters.
char myStrings[MAX_NUMBER_OF_STRING][MAX_LENGTH_OF_STRING];

Here what your are looking for:
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
char buffer[42];
const char *mytext[] = {"A1", "A2", "A3"};
scanf("%41s", buffer);
srand(time(NULL));
printf("Random text: %s\n", mytext[rand() % (sizeof(mytext) / sizeof(mytext[0]))]);
return 0;
}

#include<stdio.h>
#include<stdlib.h>
int main(){
char str[7][100]={"hello1","hello2","hello3","hello4","hello5","hello6","hello7"};
printf("%s",str[rand()%7]);
return 0;
}

Related

Functions and strings

im very new to programming, trying to learn C and cant figure out how to create/use a simple function.
Im trying to create a function called stringtest and then call it into the main and simply make the string strA print ABC.
void stringtest(char strA[20])
{
strA = "ABC";
}
int main()
{
char strA;
stringtest(strA[20]);
printf("This is strA", strA);
return 0;
}
You need to read up on pointers and the C syntax in general.
This is one way you could do it.
#include <stdio.h>
#include <string.h>
void stringtest(char *strA) {
strcpy(strA, "ABC");
}
int main(int argc, const char * argv[]) {
char strA[20];
stringtest(&strA[0]);
printf("This is strA -> %s \n", strA);
return 0;
}
Take care,
/Anders.
I don't think your code ran!!
There are a lot of bugs and errors in your code.
See the code given below to understand how to do this:
#include <stdio.h>
char strA[20];
void stringtest(){
strA[0]='A';
strA[1]='B';
strA[2]='C';
strA[3]='\0';
}
int main(){
stringtest();
printf("This is strA %s",strA);
}

Segmentation fault (core dumped) error for C program

I am trying to run below program in an online C compiler. But I get segmentation error. Can you help me fix this
#include <stdio.h>
#include <string.h>
int main()
{
char string[15] = "Strlwr in C";
printf("%s",tolower(string));
return 0;
}
Following is the prototype of tolower
int tolower(int c);
You should pass an int or something like char which can safely convert to int. Passing char * (Type of string) like you do leads to UB.
To convert a string to lowercase, you need to convert each character separately. One way to do this is:
char string[15] = "Strlwr in C";
char lstr[15];
int i = 0;
do {
lstr[i] = tolower(string[i]);
} while(lstr[i] != '\0');
printf("%s", lstr);
You are using tolower incorrectly. This function returns int and gets int as a parameter (here is it's declaration: int tolower(int c);). What you want to do is call it on each char of your char array, and print each one:
char string[15] = "Strlwr in C";
for(int i = 0; i < strlen(string); i++)
printf("%c",tolower(string[i]));
Read cplusplus.com/reference/cctype/tolower It takes a single int as parameter, not char and not array.
You probably want to use a loop on "string", which processes each in turn.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(void)
{
int i;
char string[15] = "Strlwr in C";
for (i=0; i< sizeof(string)/sizeof(char); i++)
{
string[i]=(char)(tolower((int)string[i]));
}
printf("%s\n",string);
return 0;
}
Output:
strlwr in c

How do you pass a character array

I want to pass an array of characters i.e. a String in c
int main()
{
const char c[]="Joseph";
TestWord(&c,&c);
return 0;
}
int TestWord(char tiles[], char word[])
{
return tiles;
}
#include <stdio.h>
char *TestWord(char tiles[], char word[]);
int main()
{
char c[]="Joseph";
char r;
r = *TestWord(c,c);
return 0;
}
char *TestWord(char tiles[], char word[])
{
return tiles;
}
You pass through the arrays without the & as arrays don't need those, as they are already somewhat like pointers, just like how you would scanf an array without the & symbol.
Don't forget that if you are returning tiles that you should save that in a variable.
you could pass a string(character array) in C in many ways.
This code passes the string a to the function PRINT. Note that in this method the base address of the array is sent to the function.
#include<stdio.h>
void PRINT(char b[])
{
printf("%s",b);
}
int main()
{
char a[]="hello";
PRINT(a);
return 0;
}

Array with int and char in C

I need to put 3 strings on an array[3][3].
I tried to do it with pointers, but I only receive a single character.
#include <stdio.h>
int array[3][3]
char thing[5] = "thing";
main()
{
thing = array[0][0];
printf("%s", array[0][0];
}
Try this. With due respect your code absolutely incorrect and need many changes. You need to update your programming skills too.
#include <stdio.h>
#include <string.h>
char array[3][6]={0};
char *thing = "this";
main()
{
strcpy(array[0],thing);
printf("%s\n", array[0]);
}

How to return array of string in function?

I want to create a static array of string in a function , then the return value of this function will assign to another array of string .
but it seems I can't do this assignment array2 = function(); but I think use char * for array of string is ok?
what's wrong in my code?
thanks
here is my code
#include <stdio.h>
#include <stdlib.h>
char * function();
int main(){
char * array2[100] = {};
array2 = function();
// print this array of string
int i;
for(i=0;i<strlen(array2);i++){
printf("%s\n",array2[i]);
}
system("pause");
}
char * function(){
static char * array[100] = {};
array[0] = "100";
array[1] = "200";
return array;
}
You should use char** or char* array[] for array of strings.
In your function(), array is the first address of array of char*.the prototype should be char **function().
And I don't think array's name can be changed.Say int a[3], b[3];, a is a constant, So a = b is not allowed.
Be careful about the spelling mistakes like funtion() function().
I'd do something like this:
#include <stdio.h>
#include <stdlib.h>
static char **function(void)
{
char **array = malloc(2 * sizeof(*array));
array[0] = "100";
array[1] = "200";
return array;
}
int main(int argc, char *argv[])
{
char **array2;
array2 = function();
/* print this array of string */
int i;
for (i = 0; i < 2; i++)
printf("%s\n",array2[i]);
free(array2);
return 0;
}
There are many ways to solve your problem. It seems to be similar to these questions [general arrays][1]
[String arrays][2]
Or you can pass the array instead and get it working like this
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include "string.h"
using namespace std;
void function(char *input[100]){
input[0] = "100";
input[1] = "200";
}
int main(){
char * array2[100];
function(array2);
// print this array of string (int)strlen(*array2)
int i;
for(i=0;i<(int)strlen(*array2) - 1;i++){
printf("%s\n",array2[i]);
}
system("pause");
}

Resources