I'm trying to check if a character belongs to a two dimensional array of characters, but my code doesn't work right for all characters. I think that something goes wrong with my pointer.For example if i change 'b' to 'r' it doesn't work correctly.
Thanks in advance.
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) {
char *pch;
char matrix[2][2]={
"best","cover",
"orbit","boost"
};
pch=strchr(matrix,'b');
if (pch==NULL)
{
printf ("not exists");
}
else
{
printf("exists");
}
return 0;
}
There are three problems in your code.
You need to include <stdio.h> in order to use printf.
There isn't enough memory allocated here:
char matrix[2][2]={
"best","cover",
"orbit","boost"
};
You give space for two strings each of which can hold 1 character(+1 for the NUL-terminator at the end).
Fix it by using:
char matrix[4][6]={
"best","cover",
"orbit","boost"
};
The six is the maximum number of characters in a string + one for the \0 at the end and the four is the number of maximum number of strings in the 2D array.
strchr expects the first argument to be of type const char*. You give the argument matrix which is of type char(*)[2]. You need to loop and find if the character is in each string of the array of array of chars.
After fixing these problems, your code will be:
#include <stdlib.h> // Not required
#include <string.h> // For strchr()
#include <stdio.h> // For printf()
int main(int argc, char *argv[]) {
char *pch;
char matrix[4][6]={
"best","cover",
"orbit","boost"
};
int i;
for(i=0;i<4;i++)
{
pch=strchr(matrix[i],'b');
if (pch==NULL)
{
printf ("not exists in %s\n",matrix[i]);
}
else
{
printf("exists in %s\n",matrix[i]);
}
}
return 0;
}
Related
I'm attempting to create a program that takes an input of eleven strings or things to do and stores them in an array and I want to store them such that only the last byte for each string in storage is the null character. I then want to be able to press the enter key and have the program print randomly one of these items from the strings. However, I appear to be having a hard time with memory management as the first string being stored is never the string that I stored it is always junk. I was curious why this is the case and what I am doing wrong.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(){
time_t t;
srand((unsigned) time(&t));
int num_string=11,k=0;
char** list_of_strings;
list_of_strings=malloc(4*11);
while (num_string>0){
int i=0;
char* string;
string=malloc(100);
for(i=0;i<100;i++){
scanf("%c",&string[i]);
if(string[i]=='\n'){
string[i]='\0';
break;
}
}
string=realloc(string,i+1);
list_of_strings[k]=string;
k++;
num_string--;
}
char ch;
while(1){
scanf("%c",&ch);
if(ch=='\n'){
int rand_num=rand()%11;
char*sel_string=list_of_strings[rand_num];
while(*sel_string!='\0'){
printf("%c",*sel_string);
sel_string++;
}
}
}
return 0;
}
this is the code, it is supposed to invert a string.
#include <stdio.h>
void StrRev(char str[]) {
int len=strlen(str);
char out[len];
int i;
for(i=0;i<len;i++){
out[i]=str[len-i-1];
}
printf("%s",out);
}
int main(void) {
StrRev("TestString");
return 0;
}
expected output:
gnirtStseT
actual output:
gnirtStseT#
the same thing happens with other inputs, while not on some others.
compiled with MingW on Code::Blocks
Two issues:
You fail to #include <string.h>, so there's no declaration for strlen. So the function is implicitly declared as int strlen(). So add that to the top of the file.
You also aren't adding the null terminating character to your reversed string.
After the for loop, set one additional character in the array to 0. Also, you'll need to make the array one larger to fit it.
#include <stdio.h>
// import declaration of strlen
#include <string.h>
void StrRev(char str[]) {
int len=strlen(str);
char out[len+1]; // increase length to make room for null terminator
int i;
for(i=0;i<len;i++){
out[i]=str[len-i-1];
}
out[i]=0; // add null terminator
printf("%s",out);
}
int main(void) {
StrRev("TestString");
return 0;
}
strlen() returns the length of the string without the null terminator at the end. So when you're copying over the characters, you're skipping the NULL at the end. If you simply use
int len=strlen(str) + 1;
instead of
int len=strlen(str);
Then everything should work.
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]);
}
My task is to compare some words and to find a character which is not used in both of them. Here is my code. But I'm getting a warning:
[Warning] passing argument 1 of 'ret' makes pointer from integer without a cast [enabled by default].
And when I'm trying to run it it says consolepauser.exe stopped working
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char ret(char a[1][10],char b[3][10])
{
int i,j,p,t;
for (i=0;i<1;i++)
for (j=0;j<10;j++)
for (p=0;p<3;p++)
for (t=0;t<10;t++)
{
if (tolower(a[i][j]==tolower(b[p][t])))
{
p=3;
break;
}
if (p==2)
if (t==9) return tolower(a[i][j]) ;
}
return 'N';
}
int main(int argc, char *argv[]) {
char k[3][10]={"cHaOs","TOP","blAa"};
char b[1][10]={"SomeThIng"};
char q[1][10]={"HaPa"};
if (ret(b[1][10],k[3][10])='N') printf("No character") ;
else printf("%c",ret(b[1][10],k[3][10])) ;
return 0;
}
You should pass the parameters as:
if (ret(b, k) == 'N') printf("No character");
else printf("%c", ret(b, k));
[Warning] passing argument 1 of 'ret' makes pointer from integer without a cast
b[1][10] is a char, not a variable of type char [1][10], you should call ret() like this: ret(b, k). Others are similar.
Note: the valid indexes of char b[1][10]; are b[0][0], b[0][1], ..., b[0][9], the indexes in `b[1][10]1 are out-of-bounds, and will cause undefined behavior.
Here is a syntax fixed version of your code, you may want to compare it with your original code to find out other problems in it:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char ret(char a[1][10],char b[3][10])
{
int i,j,p,t,e,r;
for (i=0;i<1;i++)
for (j=0;j<10;j++)
for (p=0;p<3;p++)
for (t=0;t<10;t++)
{
if (tolower(a[i][j])==tolower(b[p][t]))
{
p=3;
break;
}
if (p==2)
if (t==9) return tolower(a[i][j]) ;
}
return 'N';
}
int main(int argc, char *argv[]) {
int i,j,p,t,e,r;
char a,h;
char k[3][10]={"cHaOs","TOP","blAa"};
char b[1][10]={"SomeThIng"};
char q[1][10]={"HaPa"};
if (ret(b,k)=='N') printf("No character");
else printf("%c",ret(b,k));
return 0;
}
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;
}