I want to take all the letters in a string and put it in a array separately. But I am receiving some error and I could not figure out.
10 20 E:\FALL SEM 20-21\CS\C codes\Untitled3.c [Warning] passing argument 2 of 'strcpy' makes pointer from integer without a cast
My code is
#include<stdio.h>
#include<string.h>
char array[10][100],string[100];
int top=0;
void push(char elem)
{
strcpy(array[top],elem);
top++;
}
int main()
{
printf("Enter the string: \n");
fgets(string,100,stdin);
int length;
length=strlen(string);
int i=0;
while((string[i])!='\0')
{
push(string[i]);
i++;
}
printf("%d",length);
}
strcpy() is for copying strings (sequences of characters terminated by a null-character). To use that, you should make strings from the characters and pass them. Also you have to fix the type of the argument of push().
#include<stdio.h>
#include<string.h>
char array[10][100],string[100];
int top=0;
void push(const char* elem) /* use const char* to receive strings that won't be modified */
{
strcpy(array[top],elem);
top++;
}
int main(void)
{
printf("Enter the string: \n");
fgets(string,100,stdin);
int length;
length=strlen(string);
int i=0;
while((string[i])!='\0')
{
char str[2] = {string[i], '\0'}; /* create a string */
push(str); /* and push that */
i++;
}
printf("%d",length);
}
Related
So i have a piece of code where i want to store 5 random names no longer than 10 letters each
void printname(char *s);
int main() {
char NAME[10][5];
int NAMECOUNTER=0;
while(NAMECOUNTER<5) {
scanf("%s",NAME[NAMECOUNTER]);
printname(&NAME[NAMECOUNTER]);
NAMECOUNTER++;
}
}
void printname(char *s) {
printf("Hello %s\n",*s);
return;
}
And lets say i want the name to print itself through function Printname. Why does this not work and prints "Hello (null)"?
One problem I see
char NAME[10][5];
Should be
char NAME[5][11];
This way you're declaring 5 name slots, each with 10 char max. The extra space is for the string null terminator.
You have
void printname(char *s) {
printf("Hello %s\n",*s);
return;
}
and you need
void printname(char *s) {
printf("Hello %s\n",s); // Took off the * from the *s
return;
}
because s is already your pointer.
I give 6 glitches to fix. See comments prefacing #<num>
#include <stdio.h>
#include <stdlib.h>
void printname(char *s);
int main() {
char NAME[5][11] = {0}; // #1 value initialize to zero
// #2 the second dimension should be 11, since 10 letters plus null-character
int NAMECOUNTER=0;
while(NAMECOUNTER<5) {
scanf("%10s",NAME[NAMECOUNTER]); // #3 %10s width specifier, limit the length of name up to 10 character long
while ( (ret = getchar()) != '\n') ; // #4 trim exceeding characters
printname(NAME[NAMECOUNTER]); // #5 no &
NAMECOUNTER++;
}
}
void printname(char *s) {
printf("Hello %s\n",s); // #6 no *
return;
}
Your code doesn't even print anything to me, it just freeze and closes. Also it's not so clear.
You are mistyping the "NAME" array inside printname. This function does not have a return value, since it has a void return, so you shouldn't type any return. Also you can save some code lines writing it before main function which is the best practice:
void printname(){
//todo
}
int main(){
return 0;
}
This is a better way to implement your function:
void printname(char Names[5][11], int index){
printf("Hello %s\n", Name[index]);
}
You could replace the while structure with for structure, since it is the best application for the purpose in that case:
Your code piece:
while(NAMECOUNTER<5) {
scanf("%s",NAME[NAMECOUNTER]);
printname(&NAME[NAMECOUNTER]);
NAMECOUNTER++;
}
You don't have to put "&" in the first argument of printname.
Best:
for(NameCounter = 0; NameCounter < 5; NameCounter++){
scanf("%s",NAME[NAMECOUNTER]);
printname(NAME[NAMECOUNTER]);
}
Also make sure to keep your code indentation clear. This is your final code:
void printname(char Names[][11], int index){
printf("Hello %s\n", Names[index]);
}
int main(){
char NAME[5][11];
int NAMECOUNTER;;
for(NAMECOUNTER = 0; NAMECOUNTER < 5; NAMECOUNTER++){
scanf("%s", NAME[NAMECOUNTER]);
printname(NAME, NAMECOUNTER);
}
}
I have been trying to compare a structure variable and a string variable. But I am getting this error.
#include<stdio.h>
#include<string.h>
int main()
{
struct details {
char number[20];
} det[10];
int inp, i=0;
char aad;
int b;
puts("Give The Number To Display The Pass Status");
scanf("%s", &aad);
for(b=0;b<i;b++)
{
if(det[i].number==aad)
{
printf("Hello");
}
}
return 0;
}
Pls try to fix my error
after you look in the comments this will be the answer
#include<stdio.h>
#include<string.h>
#define SIZE 20 // add size for aad
int main()
{
struct details {
char number[20];
} det[10];
int inp, i = 0;
char aad[SIZE]; // must be array (can be with pointer, use malloc)
int b;
puts("Give The Number To Display The Pass Status");
scanf("%s", &aad);
for (b = 0; b < i; b++)
{
if (strcmp(det[b].number, aad) == 0) // strcmp to compare strings
{
printf("Hello");
}
}
return 0;
}
So here's code:
#include <stdio.h>
#include <stdlib.h>
void draw(int length, char brush);
int stringLength(char name[]);
void drawName(char nameLetter[], char brush);
int length;
char brush, name;
char *a = "Anne", *b = "Bart", *c = "Celine", *d = "Darius";
int main(void) {
printf("Write down the length of line.\n");
scanf("%d", &length);
printf("Give first letter of the name (a, b, c albo d)\n");
scanf(" %c", &name);
printf("Pick character to be used as brush.\n");
scanf(" %c", &brush);
drawName(name, brush);
return 0;
}
void draw(int length, char brush) {
int i;
for (i = 0; i < length; i++) {
printf("%c", brush);
}
}
int stringLength(char name[]) {
int i;
while (name[i] != '\0') {
i++;
}
return i;
}
void drawName(char nameLetter[], char brush) {
draw(stringLength(nameLetter), brush);
printf("%s\n", nameLetter);
draw(stringLength(nameLetter), brush);
}
What I'm trying to do here, is to get the length of a string and use this value as a parameter in another function responsible for printing as many characters in a row, as there are characters is in given string. But I get an error at line 19: (passing argument 1 of ‘drawName’ makes pointer from integer without a cast).. I've read all the questions simiar to this one here on stackoverflow, but still can't make it work. I'd appreciate your help a lot! I'm at a dead end here...
The function
void drawName(char nameLetter[], char brush);
is declared with the first parameter of type char *
While you are calling it with an argument of type char.
char brush, name;
^^^^^^^^^^^^^^^^
//...
drawName(name, brush);
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;
}
I am having a problem with this code, this code is a encryption for a rail cipher and if you enter in an input "testing" you should get an output "tietnsg" which i do get.
However if i change the input to "testingj" i get an output of "tietnjsgp?²!lj" i can see from my debugging the "?²!lj" appears to be tagged on during the last fill in the toCipher function
does anyone know how to fix it other than the way that i did it?
/*
CIS Computer Secutrity Program 1
10-10-14
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
char *toCipher(char **arr,int x,int y);
char *Encrypt(char *pT, int size);
char **create(int x,int y);
void FreeArr(char **array, int y);
void print(char *word,int strl);
int main(){
char pt[]= "testingj";
char *word = Encrypt(pt,3);
print(word, sizeof(pt));
free(word);
}
/*
Take in a pointer to a word, and the lenght of the string
Post print each char in the array, (used beacuase i had some issues with the memory, i keep getting extra adresses
*/
void print(char *word,int strl){
int i;
for(i=0;i<strl-1;i++){
printf("this is correct %c",word[i]);
}
printf("\n");
}
/*
Pre, take in the pointer to the plain text word to be encrypted as well as the depth of the Encryption desired
Post: Construct the array, insert values into the 2d array, convert the 2d array to a 1d array and return the 1d array
*/
char *Encrypt(char *word,int y){
int x = strlen(word);
int counter=0;
int ycomp=0;
int rate=1;
char **rail = create(x,y);
while(counter<x){
if(ycomp==y-1){
rate=-1;
}
if(ycomp==0){
rate=1;
}
rail[counter][ycomp]=word[counter];
ycomp=ycomp+rate;
counter++;
}//end of rail construction
char *DrWord = toCipher(rail,x,y);
FreeArr(rail,y);
return(DrWord);
}
/*
Create a dynamic 2d array of chars for the rail cypher to use
Take in the dimensions
return the pointer of the rails initial address, after it created the space for the rail
*/
char *toCipher(char **arr,int x,int y){
int xI =0;
int yI=0;
int counter =0;
char *word = (char*)malloc(x);
int i;
for(yI=0;yI<y;yI++){
for(xI=0;xI<x;xI++){
if(arr[xI][yI]!= 0){
word[counter]=arr[xI][yI];
counter++;
}
}
}
printf("this is the problem %s\n",word);
return(word);
}
char **create(int x, int y){
char **rail;
int i,j;
rail = malloc(sizeof(char**)*x);
for(i=0;i<x;i++){
rail[i]= (char*)malloc(y * sizeof(char*));
}
for(i=0;i<y;i++){
for(j=0;j<x;j++){
rail[j][i]= 0;
}
}
return(rail);
}
/*
Pre, take in a malloc'd array, with the height of the array
free the malloc calls one by one, and finally free the initial adress
*/
void FreeArr(char **array, int y){
int i;
for(i=0;i<y;i++){
free(array[i]);
}
free(array);
}
In toCipher, the word is printed without nul-termination. Either:
char *word = (char*)malloc(x+1); // allocate an extra char for nul.
word[x] = 0; // add the nul at the end.
or:
printf("this is the problem %.*s\n",x,word); // limit characters printed to x.
I forgot to initialize word to 0, the tagged memory if you watch it in debug mode was not being replaced, therefore was tagged along in the newly constructed string