searching in the string

searching specified number of character in a string

Page 1 of 1

9 Replies - 860 Views - Last Post: 12 April 2010 - 01:40 PM Rate Topic: -----

#1 Guest_supergirl*


Reputation:

searching in the string

Posted 12 April 2010 - 08:20 AM

hi all im stuck to my assignment

1. i want to know how to use function search in order i can search each character in a string then count the number of it.

for example: given : home alone
the output will be :

the string has 9 characters including the white space.

if i want to search character a in the string
the output will be

1 a found in the string home alone.

if i want to search y in the string the output will be

no y is found in the string home alone

so far this is the code that i made:
#include<stdio.h>
#include<string.h>

void searchCount(char*TEXT, char z);

int main(){
int string_size;
char a;

char name[]="HAPPY BIRTHDAY";
name[strlen(name)-1]='\0';
printf("find a character>>");
scanf("%c", &a);

searchCount(name,);  //this part i have to calling searchCount function but i have no idea which argument i have to pass in

printf("The string has %d characters including white space.",i);

return 0;
}

void searchCount(char *TEXT, char z){

//no idea to put in//

return count;
}




i think i have to use switch but serious i dont understand how to mix it with pointer,pls any idea?

}

This post has been edited by JackOfAllTrades: 12 April 2010 - 08:27 AM
Reason for edit:: Added code tags.


Is This A Good Question/Topic? 0

Replies To: searching in the string

#2 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: searching in the string

Posted 12 April 2010 - 08:42 AM

you should start with something along the lines

int searchCount(char *TEXT, char z)
{

 int uCount = 0;

 for (int i = 0; i < strlen(TEXT); ++i)
    if (TEXT[i]==z) uCount++;

return uCount ;
}


This post has been edited by muballitmitte: 12 April 2010 - 08:42 AM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_supergirl*


Reputation:

Re: searching in the string

Posted 12 April 2010 - 09:28 AM

thank u so much for ur reply, but i still dont get it how can i check the the number of alphabet i want to search in the string?

this command if(TEXT[i]==z)

wasnt it just compared the whole string with 1 character?

if u can explain to me it will really help for me coz im trying to understand this also
Was This Post Helpful? 0

#4 muballitmitte  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 174
  • View blog
  • Posts: 470
  • Joined: 05-November 08

Re: searching in the string

Posted 12 April 2010 - 09:51 AM

this

if(TEXT[i]==z)



should compare the character located at position i whitin the string TEXT with the character z because when you declare an array you can access its individual elements by using the [] operator. If TEXT = "TESTCHARARRAY" then TEXT[0] = T and TEXT[4]=C.
Regarding your pointer question you should try reading this there is a section called "Pointers and arrays" which should clear things up.
Was This Post Helpful? 1
  • +
  • -

#5 supergirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 10

Re: searching in the string

Posted 12 April 2010 - 11:32 AM

thanks so far this is the code that i made and i still get error.


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

void searchCount(char *TEXT, char z);

int main(){

char a;
char *TEXT="DATA STRUCTURE";
TEXT[strlen(TEXT)-1]='\0';
printf("find a character>>");
scanf("%c",&a);

searchCount(DATA,a);

          if(strcmp(TEXT,a)!=-1)
          printf("No %c is found in the string %s",a,TEXT);
          else if(strcmp(TEXT,a)==1)
          printf("%c is found in the string %s",a,TEXT);

return 0;
}

void searchCount(char *TEXT, char z){
int count=0;
int i;
for(i=0;i<strlen(TEXT);++i)
               {
               if(TEXT[i]==z)
               count++; 
               printf("The string has %d characters including white space.", count);
               }

}



the error i got:

1:c:18: warning: passing argument 2 of strcmp makes pointer from integer without a cast
1:c:22: warning: passing argument 2 of strcmp makes pointer from integer without a cast

1.i still dont get how can i compare between pointer and character?
2. my funtion is void so i cant do calculation (count) inside the function should i do in main function?
Was This Post Helpful? 0
  • +
  • -

#6 tauit_dnmd  Icon User is offline

  • D.I.C Head

Reputation: 8
  • View blog
  • Posts: 129
  • Joined: 25-March 10

Re: searching in the string

Posted 12 April 2010 - 12:06 PM

View Postsupergirl, on 12 April 2010 - 10:32 AM, said:

thanks so far this is the code that i made and i still get error.


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

void searchCount(char *TEXT, char z);

int main(){

char a;
char *TEXT="DATA STRUCTURE";
TEXT[strlen(TEXT)-1]='\0';
printf("find a character>>");
scanf("%c",&a);

searchCount(DATA,a);

          if(strcmp(TEXT,a)!=-1)
          printf("No %c is found in the string %s",a,TEXT);
          else if(strcmp(TEXT,a)==1)
          printf("%c is found in the string %s",a,TEXT);

return 0;
}

void searchCount(char *TEXT, char z){
int count=0;
int i;
for(i=0;i<strlen(TEXT);++i)
               {
               if(TEXT[i]==z)
               count++; 
               printf("The string has %d characters including white space.", count);
               }

}



the error i got:

1:c:18: warning: passing argument 2 of strcmp makes pointer from integer without a cast
1:c:22: warning: passing argument 2 of strcmp makes pointer from integer without a cast

1.i still dont get how can i compare between pointer and character?
2. my funtion is void so i cant do calculation (count) inside the function should i do in main function?

-->
searchCount(DATA,a);
-->what's DATA? i think you want do like this:
searchCount(TEXT,a);

-->And here:
strcmp(TEXT,a)!=-1
-->can not convert char to pointer?
And i have a litle thing can help you:

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

int searchCount(char *TEXT, char z);

int main()
{
	int total;
	char a;
	char *TEXT="DATA STRUCTURE";
	printf("find a character>>");
	scanf("%c",&a);
	total=searchCount(TEXT,a);
	printf("\n The string '%s' have %d character '%c'.",TEXT,total,a);
return 0;
}

int searchCount(char *TEXT, char z)
{
	int count=0;
	int i;
	for(i=0;i<strlen(TEXT);++i)
 	{
               if(TEXT[i]==z)
               count++; 
  	}
  	return count;
}


Was This Post Helpful? 0
  • +
  • -

#7 supergirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 10

Re: searching in the string

Posted 12 April 2010 - 12:27 PM

but i should the searchCount function in void type.

thats why i dont search inside the function

any tips how to do with void function?

besides i have to calculate any character that i want to search in the words.
Was This Post Helpful? 0
  • +
  • -

#8 sarmanu  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 965
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: searching in the string

Posted 12 April 2010 - 12:31 PM

Change the function return type to "void", and instead of returning count, just output it using a printing function (printf ...).
Was This Post Helpful? 0
  • +
  • -

#9 supergirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 10

Re: searching in the string

Posted 12 April 2010 - 01:05 PM

i already changed the type into void as my first code that i post, and i

wrote in function definition


void searchCount(char *TEXT, char z){
int count=0;
int i;

for(i=0;i<strlen(TEXT);i++)
{
if(TEXT[i]==z)
count++
}

//i put printf to get the output here

printf("the string '%s' have %d character",TEXT,i);
}



now how can i calculate the numbers of specified letters? should i make another function?
Was This Post Helpful? 0
  • +
  • -

#10 supergirl  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-April 10

Re: searching in the string

Posted 12 April 2010 - 01:40 PM

i tried to write this in funtion searchCount definition after i calculate the numbers of characters inside the string, but it still gave wrong calculations.

if(TEXT[i]!=z){
              total=total+TEXT[i];
              i++;
              }
printf("%d %c found in the string %s\n",total,z,TEXT);

}



is there any addition? or mistakes that i made?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1