Hello my software should print the abc but unfortunately it does not work I think the problem is related to the line 19 so if someone could tell me why this is happening I appreciate it
My code-
#include <stdio.h>
#include <string.h>
#define NUM_ABC_LET 27
void ABC(char abc[NUM_ABC_LET]);
int main()
{
char abcString[NUM_ABC_LET] = "";
ABC(abcString);
puts(abcString);
}
void ABC(char abc[NUM_ABC_LET])
{
char letter;
for (letter = 'a'; letter <= 'z'; letter++)
{
strcat(abc, letter);
}
}
If you enable warnings in your compiler (e.g. gcc -Wall -Wextra -Werror) it will tell you the problem straight away: you are using strcat in a nonsensical way.
That's because you're just writing to a copy of the string you're passing to the function. Try this:
void ABC(char *abc)
{
int n=0;
char letter;
for (letter = 'a'; letter <= 'z'; ++letter, ++n)
{
abc[n] = letter;
}
abc[n] = '\0';
}
This way, you don't write to a copy of your string, you actually write to the string itself.
The problem is that the function stdcat() expects a null terminated string as second argument.
#include <stdio.h>
#include <string.h>
#define NUM_ABC_LET 27
void ABC(char abc[NUM_ABC_LET]);
int main()
{
char abcString[NUM_ABC_LET] = "";
ABC(abcString);
puts(abcString);
}
void ABC(char abc[NUM_ABC_LET])
{
char letterStr[2];
strcpy(letterStr, "x");
for (char letter = 'a'; letter <= 'z'; letter++) {
letterStr[0] = letter;
strcat(abc, letterStr);
}
}
Much simpler is this solution:
#include <stdio.h>
#include <string.h>
#define NUM_ABC_LET 27
void ABC(char abc[NUM_ABC_LET]);
int main()
{
char abcString[NUM_ABC_LET];
ABC(abcString);
puts(abcString);
}
void ABC(char abc[NUM_ABC_LET])
{
for (int i = 0; i < 26; ++i) {
abc[i] = 'a'+i;
}
// Add the terminating null character.
abc[NUM_ABC_LET-1] = '\0';
}
The second parameter of srtcat should be a char *.
void ABC(char abc[NUM_ABC_LET])
{
char letter[2]="a";
for (; *letter <= 'z'; (*letter)++)
{
strcat(abc, letter);
}
}
Related
#include <stdio.h>
#include <string.h>
void printArray(char* p, int len)
{
for (p; p < p + len; p++)
{
printf("%c", *p);
}
printf("\n");
}
int main(void)
{
char* msg = "hi jyegq meet me at 2 :)";
printArray(msg, strlen(msg));
getchar();
return 0;
}
I've tried to switch some things but I can't understand the problem
I need to fix this code and I think it's with the print of the pointer and I don't know how to print him with no problems
You could do this
#include <stdio.h>
#include <string.h>
void printArray(char* p, int len)
{
;
for (int i = 0; i < len; i++)
{
printf("%c", *(p++));
}
printf("\n");
}
int main(void)
{
char* msg = "hi jyegq meet me at 2 :)";
printArray(msg, strlen(msg));
getchar();
return 0;
}
Use a counter so you wouldn't go somewhere you shouldn't in memory.
Your loop will go on indefinitely. Consider what will happen if you substitute the variables with their values. p > p+len will go from 0 > 0+20 to 1 > 1+20 and so on. This will always be true. A fix could be to first store the endpoint, like for (char *end = p+len; p<end; ++p)
try this code:
#include <stdio.h>
#include <string.h>
void printArray(char* tab, int len)
{
char *p;
for (p=tab; p < tab + len; p++)
{
printf("%c", *p);
}
printf("\n");
}
int main(void)
{
char* msg = "hi jyegq meet me at 2 :)";
printArray(msg, strlen(msg));
getchar();
return 0;
}
I'm making a code to remove the file name and type from a path. However, i'm receiving warnings concerning the line where i change the content from a character. How could i get rid of the warning?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *getPath(char *fullPath){
char *aux;
int a, b, c;
aux = malloc(50 * sizeof(char));
aux = fullPath;
a = strlen(aux);
for(b=0; b<a; b++){
if (aux[b] == '/'){
c = b;
}
}
for(c; c < a; c++){
///PROBLEM HERE
aux[c] = "";
}
///PROBLEM HERE
return aux;
}
int main(void) {
char C[50];
char *path, *filename;
scanf("%s", C);
path = getPath(C);
printf("%s", path);
}
aux[c] = ""; // here "" is a char *
aux is a char *, therefore aux[c] is a char (not a string "")
aux[c] = '\0';
As written in the comments, there still have other mistakes in the rest of the code: for example aux value is erased.
Tried fixing the entire code.
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
bool getPath(char *const strippedPath, const int strippedPath_buflen,const char *const fullPath){
int b=strlen(fullPath);
for(;;){
--b;
if(b<0)
return false;
if('/'==fullPath[b])
break;
}
if(strippedPath_buflen<b+1)
return false;
strncpy(strippedPath,fullPath,b);
strippedPath[b]='\0';
return true;
}
int main(void) {
for(;;){
char C[50]={};
printf("> ");
fflush(stdout);
scanf("%s",C);
if(0==strcmp("quit",C))
break;
char path[3+1]={'X','X','X','X'};
if(getPath(path,4,C))
printf("%s\n",path);
else
printf("err\n");
}
return 0;
}
> aaaa/b.txt
err
> aaa/b.txt
aaa
> a/c/b.txt
a/c
> aa/b.txt
aa
> a/b.txt
a
> a/
a
> /b.txt
> b.txt
err
> quit
#include <stdio.h>
#include <stdlib.h>
int countArrayChars(char *strArray[]){
int i=0;
while (strArray[i] != '\0'){
i++;
}
printf("%d\n", i);
return i;
}
int main(int argc, const char * argv[]) {
char *dog[] = {"dog"};
countArrayChars(dog);
For some reason, it prints "5".
Shouldn't it print 3?
I even tried to put \0 after the "g".
You declare array of string and initialize it with dog.
char *dog[] = {"dog"};
Actually it represented as
dog[0] = "Dog"; //In your case only element index with 0.
...............
...............
dog[n] = "tiger"; //If there Have n+1 element
Hence your array size is 1. Which hold constant string dog. To access it you should use dog[0].
So without less modification you can use your code as:
int countArrayChars(char *strArray[])
{
int i=0;
while (strArray[0][i] != '\0')
{
i++;
}
printf("%d\n", i);
return i;
}
int main(int argc, const char * argv[])
{
char *dog[] = {"dog"};
countArrayChars(dog);
}
Or if you want to declare a string use
char *dog = "dog";
or
char dog[] = "dog";
Please try this
#include <stdio.h>
#include <stdlib.h>
int countArrayChars(char *strArray){
int i=0;
while (strArray[i] != '\0'){
i++;
}
printf("%d\n", i);
return i;
}
int main(int argc, const char * argv[]) {
char *dog[] = "dog";
countArrayChars(dog);
}
I got this part of a C program:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void){
char *mRNA = spleissen("AUAGUAAAAGCUCUGUUUAGGAGA", "GU", "AG");
printf("mRNA: %s\n", mRNA);
free(mRNA);
return 0;
}
I have to write the function spleissen which should work like this: it cuts out a string which goes from a GU to an AG and everything in between those two. So the program output is:
mRNA: AUACUCUGAGA
I don't really know how I can cut those parts out.
I am not allowed to use includes other than stdio, string and stdlib.
char *spleissen(const char *src, const char *start, const char *end){
size_t len = strlen(src);
char *s, *e, *ret, *work;
ret = work = malloc(len + 1);
strcpy(work, src);
len = strlen(end);
while(s = strstr(work, start)){
if((e = strstr(s, end))==NULL)
break;//delete upto last?
memmove(s, e + len, strlen(e+len)+1);
work = s;
}
return ret;
}
I think you can simply do this:
char *spleissen(char *array, char *G, char *A)
{
int l=strlen(array);
int i, j=0;
char returnstr[10010];
int b=0;
for(i=0; i<l; i++)
{
if(G[0]==array[i] && G[1]==array[i+1])
{
b=1, i++;
continue;
}
else if(A[0]==array[i] && A[1]==array[i+1] && b==1)
{
b=0, i++;
continue;
}
if(b==0)
{
returnstr[j]=array[i];
j++;
}
}
return returnstr;
}
#include<stdio.h>
#include<conio.h>
#include<string.h>
char* strreverse(char*);
int main()
{
char *rev_string;
char *name="computer";
clrscr();
rev_string=strreverse(name);
printf("%s", rev_string);
getch();
return 0;
}
char* strreverse(char *name)
{
int length=strlen(name);
char *ptr;
char *rstr;
for(ptr=name+(length-1);ptr>=name;ptr--)
{
*rstr=*ptr;
printf("%c",rstr);
rstr++;
}
*(rstr)=NULL;
return rstr;
}
the above is my code. i tried to write a program for string reverse without using arrays. But i am not getting the output retupmoc. what is wrong in my code? how to insert null char in char*?
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
char* strreverse(const char*);
int main(){
char *rev_string;
char *name="computer";
clrscr();
rev_string=strreverse(name);
printf("%s\n", rev_string);
free(rev_string);
getch();
return 0;
}
char* strreverse(const char *name){
int length=strlen(name);
const char *ptr;
char *ret, *rstr = malloc(length + 1);
if(ret=rstr){
for(ptr=name+length;ptr != name;){
*rstr++ = *--ptr;
}
*rstr = '\0';
}
return ret;
}
You did not allocate memory to hold your reversed string. Try
char *rstr = calloc(1, length+1);
Also it should be
printf("%c", *rstr); // dereference
*(rstr)= '\0'; // instead of NULL
Here you find sweet and short solution for string reverse:
#include<stdio.h>
#include<string.h>
int strreverse(char* , char*);
int main()
{
char rev_string[10] = {0};
char name[10]="computer";
strreverse(name, rev_string);
printf("%s\n", rev_string);
return 0;
}
int strreverse(char *name, char *rStr)
{
int i = 0;
int length = strlen(name);
while(i < length)
{
rStr[i] = name[length-i-1];
i++;
}
return 0;
}
Try to run and have fun.