Functions and strings - c

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);
}

Related

Modifying printf output

#include <stdio.h>
int main(){
printf("asd");
return 0;
}
The task is without modifying this program, overwriting or using define, in windows environment, this program have to write out : Yourname asd Yourname.
Any idea?
You can do it like this (edited to read Yourname from stdin):
#include <stdio.h>
#include <string.h>
int main(){
printf("asd");
return 0;
}
int printf(const char *__restrict __format, ...)
{
char str[106], scs[50];
scanf("%s", &scs);
strcpy(str,scs);
puts(strcat(strcat(strcat(strcat(str," "), __format)," "),scs));
return 1;
}
Here's a working demo.
You can do it in a simple way. Global objects are created on the static memory area, they are allocated and initialized before the execution of main, and are freed after the execution of main. Here's the simple answer to your problem:
#include<iostream>
struct MyName {
public:
MyName() { printf("%s", "GaborRuszcsak\n"); }
~MyName() { printf("%s", "\nGaborRuszcsak\n"); }
};
MyName isGaborRuszcsak;
int main(int argc, char** argv){
printf("asd");
return 0;
}
Hope that helps.

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]);
}

C program. Call a function that prints a string

Is there a way to make a function that stores a string, and then call that function in int main() that displays it on the screen? I have been searching a lot and haven't found a clear example. Here is my code. I would like to be able to call it without using the if statement
#include <stdio.h>
/* function declaration */
int StrPrint(char *str);
/* main() function */
int main()
{
char str[] = "The string i am returning n";
int (*ptr)(char *str);
ptr = StrPrint;
if (!(*ptr)(str))
printf("Done!\n");
return 0;
}
/* function definition */
int StrPrint(char *str)
{
printf("%s\n", str);
return 0;
}
The code you've posted is far more complicated than the simple task you're trying to accomplish.
Why not something like this:
#include <stdio.h>
void StrPrint(char* str);
int main(void)
{
char str[] = "The string i am returning n";
StrPrint(str);
return 0;
}
void StrPrint(char* str)
{
printf("%s\n", str);
}
This conflicts slightly with your requirement, in that the function doesn't store a string it just prints out the string passed to it as an argument. But according to the code you posted, this looks like what you're trying to accomplish.
As mentioned by you if you don't like to use pointers it can be done as shown below:
#include <stdio.h>
int StrPrint(char s[])
{
printf("%s\n", s);
return 0;
}
int main()
{
char str[] = "The string i am returning n";
if (!StrPrint(str))
printf("Done!\n");
return 0;
}

Error while executing: Segmentation fault

I'm a beginner in C language. After reading the initial chapters of Ritchie's book, I wrote a program to generate random numbers and alphabets.
The program compiles fine with gcc. However on running it, it gives an error "Segmentation fault", which is incomprehensible to my limited knowledge. I'd be glad to understand what I've written wrong.
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
long int genrandom(int,int);
void randAlph(void);
char letterize(int);
int main (void) {
// char full[9];
// char part_non[4];
srand(time(0));
int i;
for (i=0;i<50;++i) {
randAlph();
};
}
long int genrandom(int mino,int maxo) {
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1);
return val;
}
void randAlph (){
int val;
char text;
val=genrandom(0,26);
// return val;
text=letterize(val);
printf("%s ,",text);
}
char letterize(int num) {
char letter='A'+num;
return letter;
}
printf("%s ,",text); is wrong - it says that text is a nul-terminated array of chars. Use
printf("%c ,", text);
instead to print your single char.
#include <stdio.h>
#include <stdlib.h>
#include "conio.h"
#include <time.h>
int genrandom(int,int);
void randAlph(void);
char letterize(int);
int main (void) {
// char full[9];
// char part_non[4];
srand(time(0));
int i;
for (i=0;i<50;++i) {
randAlph();
};
}
int genrandom(int mino,int maxo) {//changed function return type to int
int val=mino+rand()/(RAND_MAX/(maxo-mino)+1); //Be careful when you are using '/' operator with integers
return val; //returning int here why set return type to long int?
}
void randAlph (){
int val;
char text;
val=genrandom(0,26);
// return val;
text=letterize(val);
printf("%c ,",text);//Replace %s with %c
}
char letterize(int num) { //No bound checking on num eh?
char letter='A'+num;
return letter;
}
That's all I had to say. :)
Why use %s when text is char. You dont need a string type in the function. Just a char would do. Change in the function : void randAlph ()
printf("%s ,",text);
to
printf("%c ,", text);

Random string from an array in 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;
}

Resources