C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-04-2008, 01:11 PM   #1
Registered User
 
Join Date: May 2008
Posts: 4
finding length of characters in an assigned char array ??

hello , this is my first post here and i hope i can be helpful around here, so here is my question

lets say that i have a multidimensional array defined as char names[1] [1] , the first column is to write the name of the person and the second column is to write the surname. After i write the name, this is where the question begins actually, for every consonant i must replace it by 0 and for every vowel i must put 1 , for example, if i have a name such as Jack, it must be 0100 ,

i've tried to write something on my own , but it didn't work

my guess is that if i can find the number of the characters in the name entered, i can replace them by writing a for loop and a switch
charlatanksk is offline   Reply With Quote
Old 05-04-2008, 01:25 PM   #2
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,475
If you've got "char names[1] [1]" then you've already got a bug. That entire array holds as much as a single char, nothing more.
You want two names so the first number has to be at least 2.
You want names of more than zero characters long, so the second number has to be something sensible like 32.
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Old 05-04-2008, 01:29 PM   #3
Registered User
 
Join Date: May 2008
Posts: 4
sorry for that mistake, i fixed that mistake , so
what needs to be done to find the length of the name that user entered?
charlatanksk is offline   Reply With Quote
Old 05-04-2008, 01:45 PM   #4
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
strlen function should help here
__________________
If I have eight hours for cutting wood, I spend six sharpening my axe.
vart is offline   Reply With Quote
Old 05-04-2008, 02:12 PM   #5
Registered User
 
Join Date: May 2008
Posts: 4
ok , i wrote a program but it doesn't work,


i am sorry if there are terrible mistakes here, but i'm just a beginner

what i want the program to do is to have names[0] as aaa and to print out 222

Code:
#include <stdio.h>
#include <string.h>
#include <conio.h>
int main()
{
char names [1][20];
names[0]="aaa";
char amk=(char)names[0];
for(int i=0;i<=strlen(names[0]);i++){
switch(amk){
case 'a' :
printf("2");
break;
default:
printf("1");
break;
}
}
getch();
return 0;
}
it says Lvalue is required in function main
charlatanksk is offline   Reply With Quote
Old 05-04-2008, 02:29 PM   #6
Registered User
 
Join Date: Apr 2008
Posts: 278
fixed version:

Code:
...
  char names [1][20]={{"aaa"}};
  for(int i=0;i<strlen(names[0]);i++){
    char amk=names[0][i];
    switch(amk){
      case 'a' :
      printf("2");
      break;
      default:
      printf("1");
      break;
    }
  }
...
you cannot initialize a char array with a string litteral after its allocation (names[0]="aaa")
do it during its allocation or by using strcpy() (for example) after its allocation.
Be careful with indexes [0..strlen()-1] instead of [0..strlen()].
At last, you must update the 'amk' value for each loop(char amk=names[0][i]).
root4 is offline   Reply With Quote
Old 05-04-2008, 02:34 PM   #7
Registered User
 
Join Date: May 2008
Posts: 4
thank you for your help !!
i know it went a little out of topic, but i got my answer so thank you
charlatanksk is offline   Reply With Quote
Reply

Tags
array, character, count, length, replace

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Another syntax error caldeira C Programming 31 09-05-2008 01:01 AM
Sorting Linked Lists DKING89 C Programming 6 04-09-2008 07:36 AM
get keyboard and mouse events ratte Linux Programming 10 11-17-2007 05:42 PM
Program Crashing Pressure C Programming 3 04-18-2005 10:28 PM
Character arrays PsychoBrat C++ Programming 7 06-21-2002 12:02 PM


All times are GMT -6. The time now is 12:13 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22