-
Working with strings!
I want to write a function that takes two character array parameters, say string1
and string2. Both string1 and string2 contain unknown number of
characters terminated by ’ !’. My function will erase every occurrence of
string2 in string1.
Example :
Your first string : abc!
Your second string : tabehwabcsje!
New version of your second string : tabehwsje
I was able to do that for a predefined number of characters but here in this program the string size is unknown and given by the user. This is what I did, any help would be appreciated!
( I wonder how Quzah will get angry for me this time:P!)
Code:
#include<stdio.h>
#define SIZE 50
void func(char [],int ,char [],int );
int main(void)
{
char string1[SIZE],string2[SIZE];
int i,c1=0,c2=0;
printf("Your first string, enter '!' after entering your string to terminate :\n");
scanf("%s",string1);
for(i=0;string1[i]!='!';i++){
c1++;//count element number of string1
}
printf("%d\n",c1);
printf("Your second string, add '!' again to the end of your string :\n");
scanf("%s",string2);
for(i=0;string2[i]!='!';i++){
c2++;//count element number of string2
}
printf("%d\n",c2);
func(string1,c1,string2,c2);
return 0;
}
void func(char a[],int c1,char b[],int c2){
int j=0,i=0,stop=0,count=0,k=0;
for(j=0;b[j]!='!';j++){//f1
if(b[j]==a[0]){//i1
for(i=0;a[i]!='!';i++){//f2
if(b[j]==a[i]){//i2
j++;
count++;
stop=0;
printf("olley\n");
}//i2
if(b[j]!=a[i]){
j++;
stop=1;
count=0;
printf("aaaa!\n");
}
if((count==c1)&&(stop==0)){//i3
for(k=0;k!=count;k++){//f3
b[j+count]=b[j];
j++; }//f3 }//i3
}//f2
}//i1
}//f1
printf("New version of your second string is:\n");
for(j=0;b[j]!='!';j++){
printf("%c",b[j]);
}
printf( "\n");
}
-
Edit your post and replace tabs with four spaces. It's too hard to read.
Quzah.
-
I couldnt read your function so i wrote my version...
Code:
#include <stdio.h>
#include <string.h>
int remove_substring(const char strToFind[], const char strToSearch[], char strModified[], char stop)
{
int i;
int len1 = 0, len2 = 0, len3 = 0;
while(strToFind[len1] != stop && strToFind[len1] != '\0')
len1++;
while(strToSearch[len2] != stop && strToSearch[len2] != '\0')
len2++;
if( len1 > len2 || len1 == 0)
{
strcpy( strModified, strToSearch );
return len2;
}
for( i=0; i<len2; i++ )
{
if( strToSearch[i] == strToFind[0] )
if( strncmp( &strToSearch[i], strToFind, len1 ) == 0 )
{
i += len1-1;
continue;
}
strModified[len3++] = strToSearch[i];
}
strModified[len3] = '\0';
return len3;
}
int main ()
{
char str1[] = "abc!ttt";
char str2[] = "qwertabckjhsabcff!f";
char str3[50];
remove_substring(str1, str2, str3, '!');
printf("Note! strings 1,2, terminated with %c\n", '!' );
printf("%s - %s = %s \n", str2, str1, str3 );
getchar();
return 0;
}
-
I do not know why I could not post the code sorted and looking good, I just copied it from the source file then I tried to sort. Sorry about that and thanks for helping I have to work on it to understand clearly.
-
Your editor uses a monospaced font, which this forum does not happen to like. If you like tabs, use tabs. If you like spaces, use spaces. Mixng them as indentation is like mixing C and C++ -- you generally make code hard to read, and annoy others who try to read your confuddled code.
-
Spaces and tabs were changed when I copied my code to the forum, it is and were easy to read in my editor.
Here is a new version and I hope this time it will be easy to read.
Code:
#include<stdio.h>
#include<string.h>
void func(char [],int ,char [],int );
int main(void)
{
char string1[50],string2[50];
int c1,c2;
printf("Your first string, enter '!' after entering your string to terminate :\n");
scanf("%s",string1);
printf("Your second string, add '!' again to the end of your string :\n");
scanf("%s",string2);
c1=strlen(string1)-1;
c2=strlen(string2)-1;
printf("%s\n%s\n",string1,string2); // these two lines are just to check if array sizes to be passed to the function are right
printf("%d\n%d\n",c1,c2);
func(string1,c1,string2,c2);
return 0;
}
void func(char a[],int acount,char b[],int bcount){
int j=0,i=0;
for(j=0;j<bcount;j++){
if(strncmp(&b[j],a,acount)==0){
if(b[j+acount]!='!'||b[j+acount]!='\0'){
for(i=0;i<acount;i++){
b[j]=b[j+acount];
j++;
}
}
if(b[j+acount]=='!'||b[j+acount]=='\0'){
for(i=0;i<acount;i++){
b[j]='\0';
j++;
}
}
}
}
printf("%s\n",b);
}
-
This expersion will alway be true !!!
Code:
if( b[j+acount]!='!'||b[j+acount]!='\0')
lets test with j = 0 and acount = 0, then we get to test
Code:
if ( b[0] != '!' || b[0] != '\0' )
try to run this code :
Code:
char b[1];
b[0] = '!';
printf("b[0] = %c b[0] != '!' || b[0] != '\\0' = %d\n",
b[0], b[0] != '!' || b[0] != '\0');
b[0] = '\0';
printf("b[0] = %c b[0] != '!' || b[0] != '\\0' = %d\n",
b[0], b[0] != '!' || b[0] != '\0');
b[0] = 'x';
printf("b[0] = %c b[0] != '!' || b[0] != '\\0' = %d\n",
b[0], b[0] != '!' || b[0] != '\0');
The results are :
b[0] = ! b[0] != '!' || b[0] != '\0' = 1
b[0] = b[0] != '!' || b[0] != '\0' = 1
b[0] = x b[0] != '!' || b[0] != '\0' = 1
-
... Why not use the already made 'strtok'
Code:
char str[] = "now # is the time for all # good men to come to the # aid of their country";
char delims[] = "#";
char *result = NULL;
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
Result:
Code:
result is "now "
result is " is the time for all "
result is " good men to come to the "
result is " aid of their country"