is there any function to add a character at the end of a string? or algorithm?

Is the array holding the string large enough for another character? Because if so it's a simple matter of:

s[size] = c;      // Overwite the nul with a new character
s[++size] = '\0'; // Terminate the string after the new character

is a character entered by the User. example:
string1 = "blabla"
char c
string2 = "blablac"

Please post actual C code that you want to add this feature to. If you're working with a pointer to a string literal then you can't modify it.

Edited 3 Years Ago by deceptikon

I managed to solve this problem, but emerged more.
The program is to work on the database, but I am in the beginning. The program must do the following. example: read a text file and put in another text file output. The output depends on a word written by the User.
example input file:

rice milk bread
milk sugar watermelon
apple beer bread

Word choice: bread

example of output file:

rice milk bread bread_yes
milk sugar watermelon bread_no
apple beer bread bread_yes

The problem is: In the last line of the output file arise characters, example:

rice milk bread bread_yes
milk sugar watermelon bread_no
apple beer bread "Q bread_yes

Note: random characters appear.
What can it be?

Hard to tell without seeing the code you wrote. A couple possibilities: 1. the string was not null terminated, 2. buffer overrun (writing beyond the bounds of the buffer)

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <string>



void cop(char*s1, char *s2) 
{
     int i;
     for(i=0; s1[i]!='\0'; i++)
         s2[i]=s1[i];
     s2[i]='\0';   
}

void choppy( char *s )
              {
                 s[strcspn ( s, "\n" )] = ' ';
              }


void func(char string[1000], char namesearch[50], char nameend[50], char a2[1000], char a3[1000])  
{ 

            FILE* pr = fopen("r.txt", "a++");
            if(strstr (string, namesearch) || strstr (string, nameend)) 
            {
                fprintf(pr, "%s \n", a2); 
            }   
            else
            {
                fprintf(pr, "%s \n", a3); 
            }    
            fclose(pr); 



}


int main()   
{   
    char name[30];     
    printf("File: ");  
    gets(name);
    FILE* p = fopen(name, "r");  
    if(p)   
    {
        char separator[2]; 
        char ol[3] = "\n";    
        char string[1000]; 
        char nameforout[50];
        char nameend[50];
        char namesearch[50];   
        printf("Name for search: ");  
        gets(namesearch);  
        printf("separator: ");
        gets(separator);
        cop(namesearch, nameend);
        cop(namesearch, nameforout);
        strcat(namesearch, separator);
        strcat(nameend, ol);
            char nameyes[50];
            cop(nameforout, nameyes);
            strcat(nameyes,"_yes"); 
            char nameno[50];
            cop(nameforout, nameno);
            strcat(nameno,"_no");
            char a2[1000]; 
            char a3[1000];


        while(fgets(string, sizeof(string), p) != NULL )
            {              
              choppy(string);
              cop(string, a2); 
              strcat(a2, nameyes); 
              cop(string, a3); 
              strcat(a3, nameno);
              func(string, namesearch, nameend, a2, a3);
            }

        fclose(p); 
    }
    else 
         printf("\nNot open %s", name);
    printf("\nOk!!!");     
    getchar();
    return 0;
}

This is the code :s

Curious: why didn't you use standard C string function strcpy() instead of your chop()?

lines 70 and 71: increase the size of those two because they probably aren't big enough.

First tried with strcpy, but it worked (appeared the same problem), so did the cop.

post the file you are using.

This didn't give me any unusual characters at the end of the file. The changes I made are marked // <<<<<<<<<<<<<<<<<<<<<<<<<<<<

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <string>



void cop(char*s1, char *s2) 
{
    strcpy(s2,s1);   // <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//     int i;
//     for(i=0; s1[i]!='\0'; i++)
//         s2[i]=s1[i];
//     s2[i]='\0';   
}

void choppy( char *s )
{
    s[strcspn ( s, "\n" )] = '\0'; // <<<<<<<<<<<<<<<<<<<<<<<<<<<<
}


void func(char string[1000], char namesearch[50], char nameend[50], char a2[1000], char a3[1000])  
{ 

            FILE* pr = fopen("r.txt", "a++");
            if(strstr (string, namesearch) || strstr (string, nameend)) 
            {
                fprintf(pr, "%s \n", a2); 
            }   
            else
            {
                fprintf(pr, "%s \n", a3); 
            }    
            fclose(pr); 



}


int main()   
{   
    char name[30];     
    printf("File: ");  
    gets(name);
    FILE* p = fopen(name, "r");  
    if(p)   
    {
        char separator[2]; 
        char ol[3] = "\n";    
        char string[1000]; 
        char nameforout[50];
        char nameend[50];
        char namesearch[50];   
        printf("Name for search: ");  
        gets(namesearch);  
        printf("separator: ");
        gets(separator);
        cop(namesearch, nameend);
        cop(namesearch, nameforout);
        strcat(namesearch, separator);
        strcat(nameend, ol);
            char nameyes[50];
            cop(nameforout, nameyes);
            strcat(nameyes,"_yes"); 
            char nameno[50];
            cop(nameforout, nameno);
            strcat(nameno,"_no");
            char a2[2000];  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<
            char a3[2000];  // <<<<<<<<<<<<<<<<<<<<<<<<<<<<


        while(fgets(string, sizeof(string), p) != NULL )
            {              
              choppy(string);
              cop(string, a2); 
              strcat(a2, nameyes); 
              cop(string, a3); 
              strcat(a3, nameno);
              func(string, namesearch, nameend, a2, a3);
            }

        fclose(p); 
    }
    else 
         printf("\nNot open %s", name);
    printf("\nOk!!!");     
    getchar();
    return 0;
}

Edited 3 Years Ago by Ancient Dragon

If I change according to how you put it, the program will not differentiate words. example: Bread! = breadblabla. (within the function choppy).
I changed the part of the copy, and even then continued the error.
Input file:

rice milk bread
apple sweet tomato
Watermelon tomato onion
tomato cheese bread
curd chocolate bread
Chocolate Milk Watermelon

Output file:

rice milk bread bread_yes
apple sweet tomato bread_no
Watermelon tomato onion bread_no
tomato cheese bread bread_yes
curd chocolate bread bread_yes
sweet Milk Watermelon '.bread_no

the program will not differentiate words. example: Bread! = breadblabla.

The changes I made have nothing to do with that. The problem is use of strstr() on line 28.

When I make the change in choppy as you did, it treats: bread = breadblablabla , what's wrong with strstr?

strstr() doesn't look for words -- it just determins if the charactes in one strng are in another string. It doesn't care if there are other characters surrounding the string it is looking for. If you want just the word "bread" then you should add a space both before and after it, such as " bread " But that might present other problems, for example if the breat is at the end of the line and there is no space after it, or bread is immediately followed by a period (end of sentence).

Edited 3 Years Ago by Ancient Dragon

sorry, I'm confused, if I change the choppy like you wrote the output file looks like this:
Usr word: bread.

rice milk breadbread_no 
apple sweet tomatobread_no 
Watermelon tomato onionbread_no 
tomato cheese breadbread_no 
curd chocolate breadbread_no 
Chocolate Milk Watermelonbread_no 

Do you use Notepad to view the file? Dev-C++ is a pretty old and obsolete IDE/compiler. you might want to install Code::Blocks with MinGW compiler from here, it's free and current.

Edited 3 Years Ago by Ancient Dragon

Yes, I use notepad to view the file. I used code::blocks but the problem continues :s

I also used the visual studio, but the problem worsened it emerged several strange characters in the last line =/

post the file you are talking about -- attach it to you post so that the daniweb interpretor won't change it. Here is r.txt that it created for me.

Also, what is the answer to the last question, I answered with a space.

Edited 3 Years Ago by Ancient Dragon

Attachments
rice milk bread bread_yes 
apple sweet tomato bread_no 
Watermelon tomato onion bread_no 
tomato cheese bread bread_yes 
curd chocolate bread bread_yes 
Chocolate Milk Watermelon bread_no

Ready.I posted.

Attachments
rice milk bread bread_yes 
apple sweet tomato bread_no 
Watermelon tomato onion bread_no 
tomato cheese bread bread_yes 
curd chocolate bread bread_yes 
Chocolate Milk Watermelon .bread_no

Look at the last line of the file. Has a "." more. Every time I compile a different character appears

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <string>



void cop(char*s1, char *s2)
{
     strcpy(s2,s1);
}

void choppy( char *s )
              {
                 s[strcspn ( s, "\n" )] = ' ';
              }


void func(char string[1000], char namesearch[50], char nameend[50], char a2[1000], char a3[1000])
{

            FILE* pr = fopen("r.txt", "a");
            if(strstr (string, namesearch) || (strstr (string, nameend)))
            {
                fprintf(pr, "%s \n", a2);
            }
            else
            {
                fprintf(pr, "%s \n", a3);
            }

            fclose(pr);



}


int main()
{
    char name[30];
    printf("File: ");
    gets(name);
    FILE* p = fopen(name, "r");
    if(p)
    {
        char separator[2];
        char ol[3] = "\n";
        char string[1000];
        char nameforout[50];
        char nameend[50];
        char namesearch[50];
        printf("Name for search: ");
        gets(namesearch);
        printf("separator: ");
        gets(separator);
        cop(namesearch, nameend);
        cop(namesearch, nameforout);
        strcat(namesearch, separator);
        strcat(nameend, ol);
            char nameyes[50];
            cop(nameforout, nameyes);
            strcat(nameyes,"_yes");
            char nameno[50];
            cop(nameforout, nameno);
            strcat(nameno,"_no");
            char a2[2000];
            char a3[2000];


        while(fgets(string, sizeof(string), p) != NULL )
            {
              choppy(string);
              cop(string, a2);
              strcat(a2, nameyes);
              cop(string, a3);
              strcat(a3, nameno);
              func(string, namesearch, nameend, a2, a3);
            }

        fclose(p);
    }
    else
         printf("\nNot open %s", name);
    printf("\nOk!!!");
    getchar();
    return 0;
}

I'm stumped -- I don't get that . with your program.

Put some print statements in your program so that you can see the contents of variables. Maybe that will narrow down the location of the problem. something like this:

void func(char string[1000], char namesearch[50], char nameend[50], char a2[1000], char a3[1000])
{

            FILE* pr = fopen("r.txt", "a");
            printf("a2 = \"%s\", a3 = \"%s\"\n", a2, a3);
            if(strstr (string, namesearch) || (strstr (string, nameend)))
            {
                fprintf(pr, "%s \n", a2);
            }
            else
            {
                fprintf(pr, "%s \n", a3);
            }

            fclose(pr);



}

Edited 3 Years Ago by Ancient Dragon

This question has already been answered. Start a new discussion instead.