my own similar strcat() function prints only one array of chars - c

I've been trying to practice programming so I decided to try to type the strcat() function myself, or a similar one you know. I typed this code in order to proceed it and I don't know where the problem is.
#include <stdio.h>
#include <stdlib.h>
void main(){
int i, j;
char a[100]; char b[100];
printf("enter the first string\n");
scanf("%s", &a);
printf("enter the second string\n");
scanf("%s", &b);
for(i =0; i<100; i++){
if(a[i] == '\0')
break;
}
// printf("%d", i);
for(j = i; j<100; j++){
a[j+1] = b[j-i];
if(b[j-i] == '\0')
break;
}
printf("%s", a);
}
there are no syntax errors(I hope)
the compiler gives me that result: It doesn't concatenate the strings, nothing happens.
It gives me the same array the same array the user entered, Does anyone has the answer?
PS: I don't know about the pointers yet.

Implementing strcat as a "naive byte-copy loop" is not hard, just do something like this:
#include <stdio.h>
#include <string.h>
char* strdog (char* restrict s1, const char* restrict s2)
{
s1 += strlen(s1); // point at s1 null terminator and write from there
do
{
*s1 = *s2; // copy characters including s2's null terminator
s1++;
} while(*s2++ != '\0');
return s1;
}
int main(void)
{
char dst[100] = "hello ";
char src[] = "world";
strdog(dst, src);
puts(dst);
}
Professional libraries will do the copy on "aligned chunk of data" basis, to get a slight performance boost.

Related

Get words from string array in c using whitespace character

I have tried make a program which is fills up a string array with string that is given by the user and after that i run into a problem with the idea that how coud i examine that how many words are in the array. I examined the whitespace characterts but it does not work well, i guess .Because whatever the array contains ,it writes flase value : e.g its contain 2 words when there is only 1 word in it.
int db =0;
char array[255];
void string(char str[]){
printf("Enter a string:");
scanf("%s",str);
}
void words(char str[]){
int i=0;
int len = strlen(str);
while( str[i]!=len)
{
if(str[i]==' ')
db++;
i++;
}
}
int main(int argc,char** argv){
string(array);
words(array);
printf("its contain %d words",db);
}
You have a few problems, but the main is using scanf instead of gets. If you'll run this function you'll see that scanf fails to read beyond whitespace, where gets doesn't: (my input was "123 456 789")
void string(char str[]){
printf("Enter a string:");
scanf("%s",str);
printf("%s\n", str); // the string was scanned until 1st whitespace
gets(str);
printf("%s\n", str); // proper string was read
}
The 2nd thing to note is warnings. The return type of strlen() isn't int, it's size_t (man). Also, you are comparing not matching types (char != int):
void words(char str[]){
int i=0; // change to size_t because it is checked against len
int len = strlen(str); // change to size_t
while( str[i]!=len) // should be i != len because you check the location in the string against the length, not against the char itself!!
{
if(str[i]==' ')
db++;
i++;
}
}
The 3rd thing is logic. What if your string has indeed 2 words: "hello world"? you would only count white_spaces and assume you have only one word. I would add something like:
void words(char str[]){
size_t i=0;
size_t len = strlen(str);
printf("%s\n", str);
while(i!=len)
{
if(str[i]==' ')
db++;
i++;
}
if(i) // if i is positive, there must have been a white_space -> add the 1st word because it wasn't counted (Assuming the string must start with a word and not a white_space!!)
i++;
}
This is how I would rewrite it:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int db =0;
char array[255];
void string(char str[]){
printf("Enter a string:");
gets(str);
}
void words(char str[]){
size_t i = 0;
size_t len = strlen(str);
while(i != len)
{
if(str[i]==' ')
db++;
i++;
}
if(i)
i++;
}
int main(/*no need for argc, argv. You can leave them there, but I prefer to omit if not used*/){
string(array);
words(array);
printf("its contain %d words",db);
return 0;
}
Note that there are still some issues:
What happens if more than one white space is entered between 2 words?
What happens if a white space is the first char (I assumed not, but can it be?)
What happens if the input string is too long?
Analyse main() function
int main(int argc,char** argv){
string(array);
words(array);
printf("its contain %d words",db);
}
when you are calling words(array) function what is array ? nothing. if you are taking array as a global then what's the the needs of passing to function & catching with str .
Next,
void string(char str[]){
printf("Enter a string:");
scanf("%s",str);
}
modify above function as below if you want to scan strings with white spaces.
void string(char str[]){
int len;//define it
fgets(str,len,stdin);//TO READ STRINGS WITH WHITESPACES
}
Next come to your logic, what you want to achieve with while( str[i]!=len) ?
Rotate loop upto NULL & and then modify the condition for finding no of words as below.
void words(char str[]){
int i=0;
// int len = strlen(str);
while( str[i]! = NULL)
{
if(str[i]==' ' && str[i+1] !=' ')
db++;
i++;
}
}
complete program as
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void words(char str[]);
int db =0;
char array[255];
void string(char str[]){
int n;// define it
printf("Enter a string:");
fgets(str,n,stdin);// now data is there in str not in array variable thats why call words() function from here only it's a better option
words(str);// call from here bcz now you have str
}
void words(char str[]){
int i=0;
printf("string = %s \n",str);
while( str[i] != '\0')
{
if( (str[i]==' ' && str[i+1] !=' ') || str[i+1] == '\0')
db++;
i++;
}
printf("db = %d \n",db);
}
int main(int argc,char** argv){
string(array);
printf("its contain %d words",db);
}
I hope it will helps.
I thought it's the easiest one. You have to start with your counter
int db = 1;

Array of pointers to char * in c using qsort

While adding string to my pointer's array, it is being overwriten by the last one. Could anyone tell me, where's my mistake?
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int main (){
int ile = 3;
const char * slowa[ile];
for(int j = 0; j < ile; j++){
char string[30];
gets(string);
slowa[j] = string;
printf ("%s dodalem pierwsza\n",string);
}
for (int i = 0; i < ile; i++) {
printf ("%s numer %d\n",slowa[i],i);
}
return 0;
}
The answer is in the following two lines of code:
char string[30];
...
slowa[j] = string;
The assignment sets slowa[j] to the address of the same buffer, without making a copy. Hence, the last thing that you put in the buffer would be referenced by all elements of slowa[] array, up to position of j-1.
In order to fix this problem, make copies before storing values in slowa. You can use non-standard strdup, or use malloc+strcpy:
char string[30];
gets(string);
slowa[j] = malloc(strlen(string)+1);
strcpy(slowa[j], string);
In both cases you need to call free on all elements of slowa[] array to which you have assigned values in order to avoid memory leaks.
You're always pointing to array of chars which is stack variable it's locally allocated only in scope of function, possibly each declaration of string will be on the same address as previous iteration in your loop. You could either instead of using array of chars allocate memory each loop iteration or use array and then using i.e strdup allocate memory for your new string like
slowa[j] = strdup(string) :
As others have said, you need to create copies of the strings, otherwise you set the strings to the same address, and therefore they just overwrite each other.
Additionally, I think using fgets over gets is a much safer approach. This is because gets is very prone to buffer overflow, whereas with fgets, you can easily check for buffer overflow.
This is some code I wrote a while ago which is similar to what you are trying to achieve:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PTRS 3
#define STRLEN 30
int
string_cmp(const void *a, const void *b) {
const char *str1 = *(const char**)a;
const char *str2 = *(const char**)b;
return strcmp(str1, str2);
}
int
main(void) {
char *strings[PTRS];
char string[STRLEN];
int str;
size_t len, i = 0;
while (i < PTRS) {
printf("Enter a string: ");
if (fgets(string, STRLEN, stdin) == NULL) {
fprintf(stderr, "%s\n", "Error reading string");
exit(EXIT_FAILURE);
}
len = strlen(string);
if (string[len-1] == '\n') {
string[len-1] = '\0';
} else {
break;
}
strings[i] = malloc(strlen(string)+1);
if (strings[i] == NULL) {
fprintf(stderr, "%s\n", "Cannot malloc string");
exit(EXIT_FAILURE);
}
strcpy(strings[i], string);
i++;
}
qsort(strings, i, sizeof(*strings), string_cmp);
printf("\nSuccessfully read strings(in sorted order):\n");
for (str = 0; str < i; str++) {
printf("strings[%d] = %s\n", str, strings[str]);
free(strings[str]);
strings[str] = NULL;
}
return 0;
}

Weird character appears when Inversing string in C

I want to inverse a string, so I wrote the following:
#include<stdio.h>
#include<conio.h>
#include<string.h>
char *inverseString(char *s);
char *inverseString(char *s)
{
char *s1;
int i = 0;
s1 = (char*)malloc (strlen(s)+1);
int j= strlen(s)-1;
for (; j>=0; j--) // dont know why for(; j> 0; j--) not work
{
s1[j] = s[i];
i++;
}
return s1;
}
void main(void)
{
char string[30];
printf("string: ");
gets(string);
printf("inverse string is : %s",inverseString(string));
getch();
}
But the result has a weird character at the end.
How can i fix it?
Thanks
You have to put a terminating zero character at the end of s1:
s1[strlen(s)] = 0;
Also, instead of executing the "expensive" strlen(s) many times you should calculate the length just once by putting it to a variable and then you should use the variable instead of the strlen(s) call:
size_t len = strlen(s);
You should free the allocated memory block after printing its string content by calling the free() function call.
In this case you don't have to forward declare inverseString() and be a bit more brave and use a larger buffer size, instead of 30 lets use a more common value, for example 0x100.
The string must be terminated by '\0'. This is what marks the end of string.
The null character is often represented as the escape sequence \0 in source code string literals or character constants.
Here's more elegant inplace reversal:
void strrev(char *p)
{
char *q = p;
while(q && *q) ++q;
for(--q; p < q; ++p, --q)
*p = *p ^ *q,
*q = *p ^ *q,
*p = *p ^ *q;
}
int main(int argc, char **argv)
{
do {
printf("%s ", argv[argc-1]); strrev(argv[argc-1]);
printf("%s\n", argv[argc-1]);
} while(--argc);
return 0;
}
First of all, here is a string-reversing topic, where a pretty cool method is described: Reversing a string in C.
Then, I would advice not to use gets() as it might be dangerous (gets() is actually the reason for your error to occur!).
Finally, here is the code, which might work well for you:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
/* No prototype for inverseString is required here, since the routine call occurs
after its implementation */
char *inverseString(char *s)
{
char * s1;
int i, j;
s1 = (char *)malloc(strlen(s)+1);
i = 0;
for (i = 0, j = strlen(s) - 1; j>=0; i++, j--)
{
s1[i] = s[j];
}
return s1;
}
void main(void)
{
char string[30];
printf("string: ");
scanf("%[^\n]", &string);
printf("inverse string is : %s", inverseString(string));
getch();
}
Here is a good answer where I took scanf("%[^\n]", &string) construction from.
You should terminate your string with NUL character \0.
char *inverseString(char *s)
{
...
...
s1[++i] = '\0';
return s1;
}
NOTE: Do not use gets to read strings (it is removed from the standard now) rather you can use fgets.
fgets(string, 30, stdin);

C code to reverse a string using pointer

#include <stdio.h>
main()
{
char* str;
char* strrev;
int i = 0;
int j = 0;
int c;
printf("Enter the string\n");
scanf("%[^\n]%*c", str);
while (*(str + i) != '\n')
{
i++;
}
for (c = i; c >= 0; c--)
{
*(strrev + j) = *(str + c);
j++;
}
}
This is my code to reverse a string. When I compile the code , it gives an error segmentation fault . Someone plz help me understand the error and realize my mistake.
Thanks
There are many issues:
You never allocate memory for str or strrev. This is why it crashes; writing to uninitialized pointers invokes undefined behavior, often resulting in a seg fault.
You're using scanf() with a complex conversion when you could just use fgets().
You should use strlen() rather than looping to find the end of the string.
char *str,*strrev;
first your char buffers where never initialized.
http://www.cplusplus.com/reference/cstring/strlen/
second use strlen to determine string len
regards
In addition to #unwind, even if you used str[100], the loop to find the length will fail. Since str doens not have a \n, the while() loop never ends.
// bad
char str[100];
scanf("%[^\n]%*c", str);
while (*(str + i) != '\n') {
i++;
}
// better
char str[100];
scanf("%99[^\n]%*c", str);
while (*(str + i) != '\0') { // look for \0
i++;
}
Following (some of) #unwinds pointers, here is some code that will reverse a string:
(you can use scanf(), but do not need such a complicated format string)
#include<stdio.h>
main()
{
char str[260],strrev[260];
int i=0, len;
printf("Enter the string\n");
scanf("%s",str);
len = strlen(str);
for(i=1;i<len;i++)
{
strrev[i-1] = str[len-i];
}
strrev[len]=0;
;
}
If you would like to avoid using scanf, then replace it with something like:
char str[260], strrev[260];
//...
fgets(buf, sizeof(str), stdin);
[EDIT-per comment] If still want to use a char *, do something like this:
#include<stdio.h>
main()
{
char *str, *strrev;
int i=0, len;
printf("Enter the string\n");
str = malloc(260);
strrev = malloc(260);
scanf("%s",str);
len = strlen(str);
for(i=1;i<len;i++)
{
strrev[i-1] = str[len-i];
}
strrev[len]=0;
free(str);
free(strrev);
}
Note: Other than keystroke counting, I am not sure how you would determine at run-time just how many characters will be entered by a user. So in this example, 260 bytes is assumed.
#include<stdio.h>
void main()
{
void mystrrev(char *);
char str[100];
char *p1;
p1=str;
printf("Enter the string..?\n");
scanf("%s",str);
mystrrev(p1);
printf("Reverse is =%s",p1);
}
void mystrrev(char *p1)
{
char rev[100];
char *r;
r=rev;
int i=-1,j=0;
while(*p1!='\0')
{
p1++;
i++;
}
while(i>=0)
{
p1--;
*r=*p1;
r++;
j++;
i--;
}
*p1='\0';
*r='\0';
while(j-1>=0)
{
r--;
j--;
}
while(*r!='\0')
{
*p1=*r;
p1++;
r++;
}
*p1='\0';
}
simple code

Printing a C string in reverse without using pointers?

Is there a way to print a string of fixed size in reverse without using pointers?
#include<stdio.h>
main()
{
char buffer[10];
scanf("%s", buffer);
// need to print buffer in reverse without using pointers??
}
A lovely K&R function to reverse your string in-place before printing it, perhaps?
#include <stdio.h>
#include <string.h>
void strrev(char *s) {
int tmp, i, j;
for (i = 0, j = strlen(s) - 1; i < j; i++, j--) {
tmp = s[i];
s[i] = s[j];
s[j] = tmp;
}
}
int main(int argc, const char *argv[]) {
char buffer[10];
scanf("%s", buffer);
strrev(buffer);
printf("%s\n", buffer);
return 0;
}
#include<stdio.h>
main()
{
char buffer[10];
int n = scanf("%s", buffer);
// print the number of chars written to buffer
if (n != EOF) {
int len = strlen(buffer);
if (len <= 10) {
int i;
for (i = len - 1; i >= 0; i--)
printf("%c", buffer[i]);
}
}
}
Since [] is just syntactic sugar for pointers, here's a version that works completely without pointers, arrays or anything else, just one single int. You didn't say that the string has to be stored somehow. :) (Note that I use fgetc instead of a buffer and scanf).
[jkramer/sgi5k:.../c]# cat rev.c
#include <stdio.h>
#include <stdlib.h>
void read_print();
int main(void) {
fputs("Enter your string, yo! ", stdout);
read_print();
fputs("\nDone!\n", stdout);
return EXIT_SUCCESS;
}
void read_print() {
int c = fgetc(stdin);
if(c != EOF && c != '\n') {
read_print();
fputc(c, stdout);
}
}
[jkramer/sgi5k:.../c]# gcc -o rev rev.c -Wall -W -Os
[jkramer/sgi5k:.../c]# ./rev
Enter your string, yo! foobar
raboof
Done!
Here's a recursive way of doing it; technically, this is using a pointer, but I wouldn't go into language-lawyer mode with such simple tasks.
#include <stdio.h>
/* If you want it printed forward, or backward, or think of another way.. */
typedef enum {
FRONT = 1,
BACK,
} direction;
/* Technically still using a pointer...don't nitpick. */
void echo_string(char buffer[], size_t buflen, direction from)
{
/* An index into the buffer to echo, which will preserve
* its value across subsequent recursive calls.
*/
static size_t index = 0;
/* According to the specified direction, print from the front
* or the back of the buffer. Advance the index (a misnomer, I guess).
*/
if(from == FRONT) {
printf("%c", buffer[index++]);
}
else {
printf("%c", buffer[buflen - ++index]);
}
/* Are there any more characters to echo? Yes? Awesome! */
if(index != buflen) {
echo_string(buffer, buflen, from);
}
}
int main(int argc, char **argv)
{
char buffer[10];
scanf("%s", buffer);
/* Better strlen() than sizeof() here,
* but BEWARE! scanf() is DANGEROUS!
*/
echo_string(buffer, strlen(buffer), BACK);
return(0);
}
reverse(char c[], int len)
{
if( ! (len / 2))
return;
char t = c[0];
c[0] = c[len--];
c[len] = t;
reverse(c, len-1);
}
The error(s) is left as an exercise to the student.
As caf pointed out, we're still using pointers..!
Here's an other way to solve the problem (of reversing a string).
This code snippet (and probably most others) don't respect stuff like utf8. I think signines post demonstrating the K&R way was quite close to mine (:D) so I adapted mine to fit that example (and corrected some things..)
#include <stdio.h>
#include <string.h>
void strrev(char *s) {
size_t len = strlen(s) + 1;
size_t i, j;
for(i = 0; i < len / 2; i++) {
j = len-1 - i-1;
char tmp = s[j];
s[j] = s[i];
s[i] = tmp;
}
}
int main(int argc, const char *argv[]) {
char buffer[10];
scanf("%s", buffer); // Look out for an overflow ;)
strrev(buffer);
puts(buffer);
return(0);
}
You can use strrev to reverse a string.
#include <stdio.h>
#include <string.h>
main()
{
char buffer[10];
scanf("%s", buffer);
strrev(buffer);
printf("%s", buffer);
}
void outstrreverse(const char s[])
{
size_t l=strlen(s);
while( l && s!=&s[--l] )
putchar(s[l]);
if(s[0])
putchar(s[0]);
}
Because of the relationship between C strings, arrays, and pointers the exercise is rather shotty IMHO - the most idiomatic description of a "String" in C is represented by the char*, which is not an array. Your (the OPs) title and post differ in their definitions between string and char[fixed length].
The OP should read and understand this FAQ entry, and between that and the posts here: easily figure out a solution—as well as defend it to the teacher/judge if need be.
I'll comment on this: never use scanf("%s", buffer) to populate a fixed length string. If you must use scanf() to do it, please use a field width specifier: e.g. scanf("%9s", buffer); if buffer is an [10], you want a specifier of 9 because of how scanf fills the buffer: otherwise you must beware the dragons! You could also scanf by character and evade the issue with a loops bounds, but that would likely be less efficient.
#include <stdio.h>
#include <conio.h>
void reverse(char a[], int s, int sc );
void reverse(char a[], int s, int sc ){
if ((sc-s)<(s-1))
{
a[sc-s]^=a[s-1];
a[s-1]^=a[sc-s];
a[sc-s]^=a[s-1];
reverse (a, s-1, sc) ;
}
}
void main (){
char a[]="ABCDEFG";
reverse(a, 7, 7);
printf("%d",a);
getch(); //i just use it to freeze the screen
}

Resources