Creating an Array of Strings in C [closed] - c

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I am having this issue, where I use a command-line interface.
I am trying to store the input in an array of strings. But When I run my code
I get a segmentation fault?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int num, char **str) {
int i;
char owner[20];
char *keys[5];
int j = 0;
for (i = 1; i < num; i++) {
if (i == 1) {
strcpy(owner, str[i]);
printf("%s", owner);
}
else {
keys[i] = malloc(10 * sizeof(char));
strcpy(keys[j], str[i]);
printf("%s", keys[j]);
j++;
}
}
}

In the else statement you should write
else {
keys[j] = malloc( strlen( str[i] ) + 1 );
strcpy(keys[j], str[i]);
printf("%s", keys[j]);
j++;
}
Also you should provide that the array owner is large enough to store the string str[1].

Related

how can you count the "!" but not periods, spaces, or commas? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 months ago.
Improve this question
Here is my code. It works for almost all strings...except for the ones with exclamation marks. I want to count the exclamation marks as a character but not the spaces, commas, or periods. How do I do this?
#include <stdio.h>
#include <string.h>
int main(void) {
char str[51];
fgets(str, 51, stdin);
int length = strlen(str), count = 0, i;
for (i = 0; i < length-1; i++) {
if (str[i] != ',' && str[i] != ' ' && str[i] != '.') {
count++;
}
}
printf("%d\n", count);
return 0;
}
The last character of the string is not being counted.
To go through all the string you can use i < length in the for loop

Why I am not asked [i] times to input a string? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am currently doing CS - PSET2, and I wrote the code below.
Everything is working, but now that I am re-reading it, I don't understand why.
The doubt is with line 11: if(isdigit(argv [1][i])).
Let's say I write "35", first char is a number, so loop is true and it starts --> I have to write a string (Line 16).
Now program will check the second char, "5", it is a number, true --> I have to write a string again theoretically.
But why not? Why don't I get double results for everything?
#include <stdio.h>
#include <cs50.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
int main (int argc , string argv [])
{
if (argc == 2 && isdigit(argv [1]))
{
int n = strlen(argv [1]);
for (int i=0; i<n; i++)
{
if(isdigit(argv [1][i]))
{
string plaintext = get_string("Plaintext: ");
int key = atoi(argv[1]);
int l = strlen(plaintext);
for(int p=0; p <l; p++)
if isupper(plaintext[p])
{
printf("%c", (((plaintext[p] - 'A') + key) % 26) + 'A');
}
else if islower(plaintext[p])
{
printf("%c", (((plaintext[p] - 'a') + key) % 26) + 'a');
}
else
{
printf("%c", plaintext[p]) ;
}
return 0;
}
else
{
printf("Nope\n");
return 1;
}
}
}
else
{
printf("Nope\n");
return 1;
}
}
You have a return 0; statement inside the if that checks for a digit. This will cause it return before it can loop again. Just get rid of that return.

a program to reverse each word in a string( ive read the previous solutions but im looking for one using the functions i only here [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i've wrote this c program but im always receiving the same inputed sentence as an output without any change! i've split every word in the string and then reversed their position but it didnt work well! any solutions please!
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main ()
{
char A[81][81];
int t=0,j=1,k=0,l;
puts("Input a sentence (max 80 character)");
gets(A[0]);
while (A[0][t]!='\0')
{
if(A[0][t]=='\32')
{
j++;
t++;
k=0;
}
A[j][k]=A[0][t];
k++;
t++;
}
for (l=j;l>0;l--)
{
printf("%s",A[l]);
}
getch();
}
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main(void){
char A[81][81] = {0};
int t=0,j=1,k=0,l;
puts("Input a sentence (max 80 character)");
scanf("%80[^\n]", A[0]);//'gets' has already been abolished, it should explore a different way.
while (A[0][t] != '\0'){
if(A[0][t] == ' '){
++j;
k=0;
while(A[0][t] == ' ')//Skip spaces
++t;
} else {
A[j][k++] = A[0][t++];
}
}
for (l=j;l>0;l--){
printf(" %s",A[l]);
}
puts("");
getch();
}

How to check if string contains particular character? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
How can I check if a string contains a particular, given character?
Quite trivially, actually. Here is a sample snippet. This will locate first occurrence of a character in string.
#include <stdio.h>
#include <string.h>
int main()
{
const char* haystack = "self";
const char needle = 'l';
size_t len = strlen(haystack);
size_t i;
for(i = 0; i < len; i++) {
if(haystack[i] == needle) {
fprintf(stdout, "Found char '%c' in '%s' at position %d\n", needle, haystack, (i+1));
break;
}
}
return 0;
}

Eerror message in function 'main' [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Program to take the input and output the reverse of it:
#define MAX 1000
int readtext(char[],int); /*used to store text in array and
returns the size of the line*/
void reverse(char[]); /*used to reverse the text in the line and
returns 0*/
int main(void)
{
char text[MAX];
printf("Enter text, press Ctrl+d when done \n"); /*prompt user input*/
while((redtext(text, sizeof text)>0)) /*loop repeats until text size is >0*/
{
reverse(text);
printf("%s\n\n",text);
}
return 0;
}
int readtext(char a[],int len)
{
int letchar,i;
for(i=0;i<len-1 && (letchar=getchar())!=EOF && letchar!='\n';i++) /*for loop repeats until end of line*/
a[i]=letchar;
if(letchar=='\n') /*checks if letchar is \n. if true, changes it to null and returns i value*/
a[i++]=letchar;
a[i]='\0';
return i;
}
void reverse(char a[])
{
char t;
int x,y;
for(y=0;a[y]!='\0';y++) /*loop used to get the last element of the array*/
--y;
for(x=0;x<y;x++) /*loop used to reverse the array 'a'*/
{
t=a[x];
a[x]=a[y];
a[y]=t;
--y;
}
}
expected input/output:
happy birthday
yadhtrib yppah
I am getting this error message, but do not know what it means:
/tmp/ccA71SDX.o: In function `main':
1-19.c:(.text+0x63): undefined reference to `redtext'
collect2: ld returned 1 exit status
You have made a mistake in function call (redtext instead of readtext). However you can use my solution:
#include <stdio.h>
#include <strings.h>
#define MAXSTRLEN 256
void Reverse(char* str);
int main()
{
printf("Enter string below:\n");
char str[MAXSTRLEN];
fgets(str, MAXSTRLEN, stdin);
Reverse(str);
printf("Result:\n%s\n", str);
return 0;
}
void Reverse(char* str)
{
char tmp;
int length = strlen(str) - 1;
int i;
for(i = 0; i < length / 2; i++)
{
tmp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = tmp;
}
}

Resources